

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/fluid.png">
  <link rel="icon" href="/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="深海火锅店">
  <meta name="keywords" content="">
  
    <meta name="description" content="elasticsearch学习索引库操作下载sql文件，执行sql 执行sql，创建表 sql文件 进入kibana 进入kibana  在 Elasticsearch 中，创建文档、索引和 mapping 的顺序如下：  创建索引：在 Elasticsearch 中，索引是存储文档的容器。要创建索引，可以使用 PUT 请求，指定索引名称和一些可选的设置。  创建 mapping：mapping">
<meta property="og:type" content="article">
<meta property="og:title" content="elasticsearch学习">
<meta property="og:url" content="http://example.com/2023/05/30/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/Java/%E5%AD%A6%E4%B9%A0/%E6%A1%86%E6%9E%B6/Elasticsearct/Elasticsearct%E5%AD%A6%E4%B9%A0/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="elasticsearch学习索引库操作下载sql文件，执行sql 执行sql，创建表 sql文件 进入kibana 进入kibana  在 Elasticsearch 中，创建文档、索引和 mapping 的顺序如下：  创建索引：在 Elasticsearch 中，索引是存储文档的容器。要创建索引，可以使用 PUT 请求，指定索引名称和一些可选的设置。  创建 mapping：mapping">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gitee.com/tongstyle/blog-img/raw/master/img/202307132141026.png">
<meta property="og:image" content="https://gitee.com/tongstyle/blog-img/raw/master/img/202307132141404.png">
<meta property="og:image" content="https://gitee.com/tongstyle/blog-img/raw/master/img/202307132141241.png">
<meta property="og:image" content="https://gitee.com/tongstyle/blog-img/raw/master/img/202307132141441.png">
<meta property="og:image" content="https://gitee.com/tongstyle/blog-img/raw/master/img/202307132141743.png">
<meta property="og:image" content="https://gitee.com/tongstyle/blog-img/raw/master/img/202307132141049.png">
<meta property="og:image" content="https://gitee.com/tongstyle/blog-img/raw/master/img/202307132141135.png">
<meta property="og:image" content="https://gitee.com/tongstyle/blog-img/raw/master/img/202307132141829.png">
<meta property="og:image" content="https://gitee.com/tongstyle/blog-img/raw/master/img/202307132141653.png">
<meta property="og:image" content="https://gitee.com/tongstyle/blog-img/raw/master/img/202307132142573.png">
<meta property="og:image" content="https://gitee.com/tongstyle/blog-img/raw/master/img/202307132142688.png">
<meta property="og:image" content="https://gitee.com/tongstyle/blog-img/raw/master/img/202307132142348.png">
<meta property="og:image" content="https://gitee.com/tongstyle/blog-img/raw/master/img/202307132142284.png">
<meta property="og:image" content="https://gitee.com/tongstyle/blog-img/raw/master/img/202307132142645.png">
<meta property="og:image" content="https://gitee.com/tongstyle/blog-img/raw/master/img/202307132142794.png">
<meta property="article:published_time" content="2023-05-29T16:00:00.000Z">
<meta property="article:modified_time" content="2024-04-01T12:38:20.527Z">
<meta property="article:author" content="深海火锅店">
<meta property="article:tag" content="elasticsearch">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://gitee.com/tongstyle/blog-img/raw/master/img/202307132141026.png">
  
  
    <meta name="referrer" content="no-referrer-when-downgrade">
  
  
  <title>elasticsearch学习 - Hexo</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.9.3","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.3.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>Fluid</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/background.jpg') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="elasticsearch学习"></span>
          
        </div>

        
          
  <div class="mt-3">
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2023-05-30 00:00" pubdate>
          2023年5月30日 凌晨
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          48k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          404 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">elasticsearch学习</h1>
            
            
              <div class="markdown-body">
                
                <h1 id="elasticsearch学习"><a href="#elasticsearch学习" class="headerlink" title="elasticsearch学习"></a>elasticsearch学习</h1><h2 id="索引库操作"><a href="#索引库操作" class="headerlink" title="索引库操作"></a>索引库操作</h2><h3 id="下载sql文件，执行sql"><a href="#下载sql文件，执行sql" class="headerlink" title="下载sql文件，执行sql"></a>下载sql文件，执行sql</h3><blockquote>
<h4 id="执行sql，创建表"><a href="#执行sql，创建表" class="headerlink" title="执行sql，创建表"></a>执行sql，创建表</h4></blockquote>
<p><a target="_blank" rel="noopener" href="https://www.123pan.com/s/tMU0Vv-OvzUd.html">sql文件</a></p>
<h3 id="进入kibana"><a href="#进入kibana" class="headerlink" title="进入kibana"></a>进入kibana</h3><blockquote>
<h1 id="进入kibana-1"><a href="#进入kibana-1" class="headerlink" title="进入kibana"></a><strong>进入<a target="_blank" rel="noopener" href="http://localhost:5601/app/dev_tools#/console"><code>kibana</code></a></strong></h1></blockquote>
<blockquote>
<p>在 Elasticsearch 中，创建文档、索引和 mapping 的顺序如下：</p>
<ol>
<li><p>创建索引：在 Elasticsearch 中，索引是存储文档的容器。要创建索引，可以使用 PUT 请求，指定索引名称和一些可选的设置。</p>
</li>
<li><p>创建 mapping：mapping 定义了索引中的字段和它们的数据类型。要创建 mapping，可以使用 PUT 请求，指定索引名称和 mapping 定义。</p>
</li>
<li><p>创建文档：文档是索引中的数据单元。要创建文档，可以使用 POST 请求，指定索引名称、文档 ID 和文档内容。</p>
</li>
</ol>
<p>  <code>需要注意的是</code>，如果在创建文档时指定的索引不存在，Elasticsearch 会自动创建该索引，并使用默认的 mapping。因此，为了确保索引中的字段和数据类型符合预期，最好在创建索引之前先定义好 mapping。</p>
</blockquote>
<p><img src="https://gitee.com/tongstyle/blog-img/raw/master/img/202307132141026.png" srcset="/img/loading.gif" lazyload alt="image-20230525145544098"></p>
<img src="https://gitee.com/tongstyle/blog-img/raw/master/img/202307132141404.png" srcset="/img/loading.gif" lazyload alt="image-20230525150351576" style="zoom:80%;" />

<blockquote>
<h3 id="酒店定义如下，但是注意一个问题"><a href="#酒店定义如下，但是注意一个问题" class="headerlink" title="酒店定义如下，但是注意一个问题"></a>酒店定义如下，但是<code>注意一个问题</code></h3><p>name、brand、business等字段都要参与搜索，也就是说用户输入的关键词可能是多个关键字（查询条件不是一个值，而是多个值）</p>
<p>可以使用<code>copy_to</code>属性，将当前字段拷贝到指定字段</p>
</blockquote>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><code class="hljs sql"># 酒店的mapping映射（不完美）<br>PUT <span class="hljs-operator">/</span>hotel<br>&#123;<br>  &quot;mappings&quot;: &#123;<br>    &quot;properties&quot;: &#123;<br>      &quot;id&quot;: &#123;<br>        &quot;type&quot;: &quot;keyword&quot;<br>      &#125;,<br>      &quot;name&quot;: &#123;<br>        &quot;type&quot;: &quot;text&quot;,<br>        &quot;analyzer&quot;: &quot;ik_max_word&quot;<br>      &#125;,<br>      &quot;address&quot;: &#123;<br>        &quot;type&quot;: &quot;keyword&quot;,<br>        &quot;index&quot;: <span class="hljs-literal">false</span><br>      &#125;,<br>      &quot;price&quot;:&#123;<br>        &quot;type&quot;: &quot;integer&quot;<br>      &#125;,<br>      &quot;score&quot;:&#123;<br>        &quot;type&quot;: &quot;integer&quot;<br>      &#125;,<br>      &quot;brand&quot;:&#123;<br>        &quot;type&quot;: &quot;keyword&quot;<br>      &#125;,<br>      &quot;city&quot;:&#123;<br>        &quot;type&quot;: &quot;keyword&quot;<br>      &#125;,<br>      &quot;starName&quot;:&#123;<br>        &quot;type&quot;: &quot;keyword&quot;<br>      &#125;,<br>      &quot;business&quot;:&#123;<br>        &quot;type&quot;:&quot;keyword&quot;<br>      &#125;,<br>      &quot;location&quot;:&#123;<br>        &quot;type&quot;:&quot;geo_point&quot;<br>      &#125;,<br>      &quot;pic&quot;:&#123;<br>        &quot;type&quot;:&quot;keyword&quot;,<br>        &quot;index&quot;:<span class="hljs-literal">false</span><br>      &#125;<br>    &#125;<br>  &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<blockquote>
<h1 id="更改后的mapping如下"><a href="#更改后的mapping如下" class="headerlink" title="更改后的mapping如下"></a>更改后的mapping如下</h1></blockquote>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><code class="hljs sql"># 酒店的mapping映射<br>PUT <span class="hljs-operator">/</span>hotel<br>&#123;<br>  &quot;mappings&quot;: &#123;<br>    &quot;properties&quot;: &#123;<br>      &quot;id&quot;: &#123;<br>        &quot;type&quot;: &quot;keyword&quot;<br>      &#125;,<br>      &quot;name&quot;: &#123;<br>        &quot;type&quot;: &quot;text&quot;,<br>        &quot;analyzer&quot;: &quot;ik_max_word&quot;,<br>        &quot;copy_to&quot;:&quot;all&quot;<br>      &#125;,<br>      &quot;address&quot;: &#123;<br>        &quot;type&quot;: &quot;keyword&quot;,<br>        &quot;index&quot;: <span class="hljs-literal">false</span><br>      &#125;,<br>      &quot;price&quot;:&#123;<br>        &quot;type&quot;: &quot;integer&quot;<br>      &#125;,<br>      &quot;score&quot;:&#123;<br>        &quot;type&quot;: &quot;integer&quot;<br>      &#125;,<br>      &quot;brand&quot;:&#123;<br>        &quot;type&quot;: &quot;keyword&quot;,<br>        &quot;copy_to&quot;:&quot;all&quot;<br>      &#125;,<br>      &quot;city&quot;:&#123;<br>        &quot;type&quot;: &quot;keyword&quot;<br>      &#125;,<br>      &quot;starName&quot;:&#123;<br>        &quot;type&quot;: &quot;keyword&quot;<br>      &#125;,<br>      &quot;business&quot;:&#123;<br>        &quot;type&quot;:&quot;keyword&quot;,<br>        &quot;copy_to&quot;:&quot;all&quot;<br>      &#125;,<br>      &quot;location&quot;:&#123;<br>        &quot;type&quot;:&quot;geo_point&quot;<br>      &#125;,<br>      &quot;pic&quot;:&#123;<br>        &quot;type&quot;:&quot;keyword&quot;,<br>        &quot;index&quot;:<span class="hljs-literal">false</span><br>      &#125;,<br>      &quot;all&quot;: &#123;<br>        &quot;type&quot;: &quot;text&quot;,<br>        &quot;analyzer&quot;: &quot;ik_max_word&quot;<br>      &#125;<br>    &#125;<br>  &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p><img src="https://gitee.com/tongstyle/blog-img/raw/master/img/202307132141241.png" srcset="/img/loading.gif" lazyload alt="image-20230525152524846"></p>
<h3 id="创建项目"><a href="#创建项目" class="headerlink" title="创建项目"></a>创建项目</h3><blockquote>
<h1 id="可以看到我这里的版本是8-6-2"><a href="#可以看到我这里的版本是8-6-2" class="headerlink" title="可以看到我这里的版本是8.6.2"></a>可以看到我这里的版本是<code>8.6.2</code></h1></blockquote>
<p><img src="https://gitee.com/tongstyle/blog-img/raw/master/img/202307132141441.png" srcset="/img/loading.gif" lazyload alt="image-20230525152822182"></p>
<blockquote>
<p>pom.xml</p>
</blockquote>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-meta">&lt;?xml version=<span class="hljs-string">&quot;1.0&quot;</span> encoding=<span class="hljs-string">&quot;UTF-8&quot;</span>?&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">project</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">&quot;http://maven.apache.org/POM/4.0.0&quot;</span> <span class="hljs-attr">xmlns:xsi</span>=<span class="hljs-string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span><br><span class="hljs-tag">         <span class="hljs-attr">xsi:schemaLocation</span>=<span class="hljs-string">&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">modelVersion</span>&gt;</span>4.0.0<span class="hljs-tag">&lt;/<span class="hljs-name">modelVersion</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>com.example<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>demo<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>0.0.1-SNAPSHOT<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">name</span>&gt;</span>demo<span class="hljs-tag">&lt;/<span class="hljs-name">name</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">description</span>&gt;</span>demo<span class="hljs-tag">&lt;/<span class="hljs-name">description</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">properties</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">java.version</span>&gt;</span>1.8<span class="hljs-tag">&lt;/<span class="hljs-name">java.version</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">project.build.sourceEncoding</span>&gt;</span>UTF-8<span class="hljs-tag">&lt;/<span class="hljs-name">project.build.sourceEncoding</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">project.reporting.outputEncoding</span>&gt;</span>UTF-8<span class="hljs-tag">&lt;/<span class="hljs-name">project.reporting.outputEncoding</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">spring-boot.version</span>&gt;</span>2.3.12.RELEASE<span class="hljs-tag">&lt;/<span class="hljs-name">spring-boot.version</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">properties</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">dependencies</span>&gt;</span><br>        <span class="hljs-comment">&lt;!--elasticsearch--&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.springframework.boot<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>spring-boot-starter-data-elasticsearch<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br>        <span class="hljs-comment">&lt;!--mybatis plus--&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>com.baomidou<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>mybatis-plus-boot-starter<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>3.5.2<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br>        <span class="hljs-comment">&lt;!--fastjson--&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>com.alibaba<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>fastjson<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>1.2.58<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br>        <span class="hljs-comment">&lt;!--web--&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.springframework.boot<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>spring-boot-starter-web<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br>        <span class="hljs-comment">&lt;!--mysql--&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>mysql<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>mysql-connector-java<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">scope</span>&gt;</span>runtime<span class="hljs-tag">&lt;/<span class="hljs-name">scope</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br>        <span class="hljs-comment">&lt;!--lombok--&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.projectlombok<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>lombok<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">optional</span>&gt;</span>true<span class="hljs-tag">&lt;/<span class="hljs-name">optional</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br>        <span class="hljs-comment">&lt;!--test--&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.springframework.boot<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>spring-boot-starter-test<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">scope</span>&gt;</span>test<span class="hljs-tag">&lt;/<span class="hljs-name">scope</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">exclusions</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">exclusion</span>&gt;</span><br>                    <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.junit.vintage<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>                    <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>junit-vintage-engine<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>                <span class="hljs-tag">&lt;/<span class="hljs-name">exclusion</span>&gt;</span><br>            <span class="hljs-tag">&lt;/<span class="hljs-name">exclusions</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">dependencies</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">dependencyManagement</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">dependencies</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.springframework.boot<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>spring-boot-dependencies<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>$&#123;spring-boot.version&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">type</span>&gt;</span>pom<span class="hljs-tag">&lt;/<span class="hljs-name">type</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">scope</span>&gt;</span>import<span class="hljs-tag">&lt;/<span class="hljs-name">scope</span>&gt;</span><br>            <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">dependencies</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">dependencyManagement</span>&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">build</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">plugins</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">plugin</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.apache.maven.plugins<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>maven-compiler-plugin<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>3.8.1<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">configuration</span>&gt;</span><br>                    <span class="hljs-tag">&lt;<span class="hljs-name">source</span>&gt;</span>1.8<span class="hljs-tag">&lt;/<span class="hljs-name">source</span>&gt;</span><br>                    <span class="hljs-tag">&lt;<span class="hljs-name">target</span>&gt;</span>1.8<span class="hljs-tag">&lt;/<span class="hljs-name">target</span>&gt;</span><br>                    <span class="hljs-tag">&lt;<span class="hljs-name">encoding</span>&gt;</span>UTF-8<span class="hljs-tag">&lt;/<span class="hljs-name">encoding</span>&gt;</span><br>                <span class="hljs-tag">&lt;/<span class="hljs-name">configuration</span>&gt;</span><br>            <span class="hljs-tag">&lt;/<span class="hljs-name">plugin</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">plugin</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.springframework.boot<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>spring-boot-maven-plugin<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>$&#123;spring-boot.version&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">configuration</span>&gt;</span><br>                    <span class="hljs-tag">&lt;<span class="hljs-name">mainClass</span>&gt;</span>com.example.demo.DemoApplication<span class="hljs-tag">&lt;/<span class="hljs-name">mainClass</span>&gt;</span><br>                    <span class="hljs-tag">&lt;<span class="hljs-name">skip</span>&gt;</span>true<span class="hljs-tag">&lt;/<span class="hljs-name">skip</span>&gt;</span><br>                <span class="hljs-tag">&lt;/<span class="hljs-name">configuration</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">executions</span>&gt;</span><br>                    <span class="hljs-tag">&lt;<span class="hljs-name">execution</span>&gt;</span><br>                        <span class="hljs-tag">&lt;<span class="hljs-name">id</span>&gt;</span>repackage<span class="hljs-tag">&lt;/<span class="hljs-name">id</span>&gt;</span><br>                        <span class="hljs-tag">&lt;<span class="hljs-name">goals</span>&gt;</span><br>                            <span class="hljs-tag">&lt;<span class="hljs-name">goal</span>&gt;</span>repackage<span class="hljs-tag">&lt;/<span class="hljs-name">goal</span>&gt;</span><br>                        <span class="hljs-tag">&lt;/<span class="hljs-name">goals</span>&gt;</span><br>                    <span class="hljs-tag">&lt;/<span class="hljs-name">execution</span>&gt;</span><br>                <span class="hljs-tag">&lt;/<span class="hljs-name">executions</span>&gt;</span><br>            <span class="hljs-tag">&lt;/<span class="hljs-name">plugin</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">plugins</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">build</span>&gt;</span><br><br><span class="hljs-tag">&lt;/<span class="hljs-name">project</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h3 id="编写测试类"><a href="#编写测试类" class="headerlink" title="编写测试类"></a>编写测试类</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> lombok.extern.slf4j.Slf4j;<br><span class="hljs-keyword">import</span> org.apache.http.HttpHost;<br><span class="hljs-keyword">import</span> org.elasticsearch.client.RestClient;<br><span class="hljs-keyword">import</span> org.elasticsearch.client.RestHighLevelClient;<br><span class="hljs-keyword">import</span> org.junit.jupiter.api.AfterEach;<br><span class="hljs-keyword">import</span> org.junit.jupiter.api.BeforeEach;<br><span class="hljs-keyword">import</span> org.junit.jupiter.api.Test;<br><span class="hljs-keyword">import</span> org.springframework.boot.test.context.SpringBootTest;<br><br><span class="hljs-keyword">import</span> java.io.IOException;<br><br><span class="hljs-meta">@Slf4j</span><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">HotelDocumentTest</span> &#123;<br><br>    <span class="hljs-keyword">private</span> RestHighLevelClient client;<br><br>    <span class="hljs-meta">@BeforeEach</span><br>    <span class="hljs-keyword">void</span> <span class="hljs-title function_">initClient</span><span class="hljs-params">()</span>&#123;<br>        <span class="hljs-built_in">this</span>.client=<span class="hljs-keyword">new</span> <span class="hljs-title class_">RestHighLevelClient</span>(RestClient.builder(<br>                HttpHost.create(<span class="hljs-string">&quot;http://localhost:9200&quot;</span>)<br>                <span class="hljs-comment">// 若是集群，可指定多个地址如下</span><br>                <span class="hljs-comment">//HttpHost.create(&quot;http://localhost:9200&quot;),</span><br>                <span class="hljs-comment">//HttpHost.create(&quot;http://localhost:9200&quot;)</span><br>        ));<br>    &#125;<br><br>    <span class="hljs-meta">@AfterEach</span><br>    <span class="hljs-keyword">void</span> <span class="hljs-title function_">destroyClient</span><span class="hljs-params">()</span>&#123;<br>        <span class="hljs-keyword">try</span> &#123;<br>            <span class="hljs-built_in">this</span>.client.close();<br>        &#125; <span class="hljs-keyword">catch</span> (IOException e) &#123;<br>            log.error(<span class="hljs-string">&quot;关闭资源出错,&#123;&#125;&quot;</span>,e.getMessage());<br>            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RuntimeException</span>(e);<br>        &#125;<br>    &#125;<br><br>    <span class="hljs-meta">@Test</span><br>    <span class="hljs-keyword">void</span> <span class="hljs-title function_">contextLoads</span><span class="hljs-params">()</span> &#123;<br>        System.out.println(client);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p><img src="https://gitee.com/tongstyle/blog-img/raw/master/img/202307132141743.png" srcset="/img/loading.gif" lazyload alt="image-20230525155822926"></p>
<blockquote>
<p>存放静态常量</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">HotelContants</span> &#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> String MAPPING_TEMPLATE=<span class="hljs-string">&quot;&#123;\n&quot;</span> +<br>            <span class="hljs-string">&quot;  \&quot;mappings\&quot;: &#123;\n&quot;</span> +<br>            <span class="hljs-string">&quot;    \&quot;properties\&quot;: &#123;\n&quot;</span> +<br>            <span class="hljs-string">&quot;      \&quot;id\&quot;: &#123;\n&quot;</span> +<br>            <span class="hljs-string">&quot;        \&quot;type\&quot;: \&quot;keyword\&quot;\n&quot;</span> +<br>            <span class="hljs-string">&quot;      &#125;,\n&quot;</span> +<br>            <span class="hljs-string">&quot;      \&quot;name\&quot;: &#123;\n&quot;</span> +<br>            <span class="hljs-string">&quot;        \&quot;type\&quot;: \&quot;text\&quot;,\n&quot;</span> +<br>            <span class="hljs-string">&quot;        \&quot;analyzer\&quot;: \&quot;ik_max_word\&quot;,\n&quot;</span> +<br>            <span class="hljs-string">&quot;        \&quot;copy_to\&quot;:\&quot;all\&quot;\n&quot;</span> +<br>            <span class="hljs-string">&quot;      &#125;,\n&quot;</span> +<br>            <span class="hljs-string">&quot;      \&quot;address\&quot;: &#123;\n&quot;</span> +<br>            <span class="hljs-string">&quot;        \&quot;type\&quot;: \&quot;keyword\&quot;,\n&quot;</span> +<br>            <span class="hljs-string">&quot;        \&quot;index\&quot;: false\n&quot;</span> +<br>            <span class="hljs-string">&quot;      &#125;,\n&quot;</span> +<br>            <span class="hljs-string">&quot;      \&quot;price\&quot;:&#123;\n&quot;</span> +<br>            <span class="hljs-string">&quot;        \&quot;type\&quot;: \&quot;integer\&quot;\n&quot;</span> +<br>            <span class="hljs-string">&quot;      &#125;,\n&quot;</span> +<br>            <span class="hljs-string">&quot;      \&quot;score\&quot;:&#123;\n&quot;</span> +<br>            <span class="hljs-string">&quot;        \&quot;type\&quot;: \&quot;integer\&quot;\n&quot;</span> +<br>            <span class="hljs-string">&quot;      &#125;,\n&quot;</span> +<br>            <span class="hljs-string">&quot;      \&quot;brand\&quot;:&#123;\n&quot;</span> +<br>            <span class="hljs-string">&quot;        \&quot;type\&quot;: \&quot;keyword\&quot;,\n&quot;</span> +<br>            <span class="hljs-string">&quot;        \&quot;copy_to\&quot;:\&quot;all\&quot;\n&quot;</span> +<br>            <span class="hljs-string">&quot;      &#125;,\n&quot;</span> +<br>            <span class="hljs-string">&quot;      \&quot;city\&quot;:&#123;\n&quot;</span> +<br>            <span class="hljs-string">&quot;        \&quot;type\&quot;: \&quot;keyword\&quot;\n&quot;</span> +<br>            <span class="hljs-string">&quot;      &#125;,\n&quot;</span> +<br>            <span class="hljs-string">&quot;      \&quot;starName\&quot;:&#123;\n&quot;</span> +<br>            <span class="hljs-string">&quot;        \&quot;type\&quot;: \&quot;keyword\&quot;\n&quot;</span> +<br>            <span class="hljs-string">&quot;      &#125;,\n&quot;</span> +<br>            <span class="hljs-string">&quot;      \&quot;business\&quot;:&#123;\n&quot;</span> +<br>            <span class="hljs-string">&quot;        \&quot;type\&quot;:\&quot;keyword\&quot;,\n&quot;</span> +<br>            <span class="hljs-string">&quot;        \&quot;copy_to\&quot;:\&quot;all\&quot;\n&quot;</span> +<br>            <span class="hljs-string">&quot;      &#125;,\n&quot;</span> +<br>            <span class="hljs-string">&quot;      \&quot;location\&quot;:&#123;\n&quot;</span> +<br>            <span class="hljs-string">&quot;        \&quot;type\&quot;:\&quot;geo_point\&quot;\n&quot;</span> +<br>            <span class="hljs-string">&quot;      &#125;,\n&quot;</span> +<br>            <span class="hljs-string">&quot;      \&quot;pic\&quot;:&#123;\n&quot;</span> +<br>            <span class="hljs-string">&quot;        \&quot;type\&quot;:\&quot;keyword\&quot;,\n&quot;</span> +<br>            <span class="hljs-string">&quot;        \&quot;index\&quot;:false\n&quot;</span> +<br>            <span class="hljs-string">&quot;      &#125;,\n&quot;</span> +<br>            <span class="hljs-string">&quot;      \&quot;all\&quot;: &#123;\n&quot;</span> +<br>            <span class="hljs-string">&quot;        \&quot;type\&quot;: \&quot;text\&quot;,\n&quot;</span> +<br>            <span class="hljs-string">&quot;        \&quot;analyzer\&quot;: \&quot;ik_max_word\&quot;\n&quot;</span> +<br>            <span class="hljs-string">&quot;      &#125;\n&quot;</span> +<br>            <span class="hljs-string">&quot;    &#125;\n&quot;</span> +<br>            <span class="hljs-string">&quot;  &#125;\n&quot;</span> +<br>            <span class="hljs-string">&quot;&#125;&quot;</span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<blockquote>
<p>编写测试方法</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> com.example.demo.constants.HotelContants;<br><span class="hljs-keyword">import</span> lombok.extern.slf4j.Slf4j;<br><span class="hljs-keyword">import</span> org.apache.http.HttpHost;<br><span class="hljs-keyword">import</span> org.elasticsearch.client.RequestOptions;<br><span class="hljs-keyword">import</span> org.elasticsearch.client.RestClient;<br><span class="hljs-keyword">import</span> org.elasticsearch.client.RestHighLevelClient;<br><span class="hljs-keyword">import</span> org.elasticsearch.client.indices.CreateIndexRequest;<br><span class="hljs-keyword">import</span> org.elasticsearch.common.xcontent.XContentType;<br><span class="hljs-keyword">import</span> org.junit.jupiter.api.AfterEach;<br><span class="hljs-keyword">import</span> org.junit.jupiter.api.BeforeEach;<br><span class="hljs-keyword">import</span> org.junit.jupiter.api.Test;<br><span class="hljs-keyword">import</span> org.springframework.boot.test.context.SpringBootTest;<br><br><span class="hljs-keyword">import</span> java.io.IOException;<br><br><span class="hljs-meta">@Slf4j</span><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">HotelDocumentTest</span> &#123;<br><br>    <span class="hljs-keyword">private</span> RestHighLevelClient client;<br><br>    <span class="hljs-meta">@BeforeEach</span><br>    <span class="hljs-keyword">void</span> <span class="hljs-title function_">initClient</span><span class="hljs-params">()</span>&#123;<br>        <span class="hljs-built_in">this</span>.client=<span class="hljs-keyword">new</span> <span class="hljs-title class_">RestHighLevelClient</span>(RestClient.builder(<br>                HttpHost.create(<span class="hljs-string">&quot;http://localhost:9200&quot;</span>)<br>                <span class="hljs-comment">// 若是集群，可指定多个地址如下</span><br>                <span class="hljs-comment">//HttpHost.create(&quot;http://localhost:9200&quot;),</span><br>                <span class="hljs-comment">//HttpHost.create(&quot;http://localhost:9200&quot;)</span><br>        ));<br>    &#125;<br><br>    <span class="hljs-meta">@AfterEach</span><br>    <span class="hljs-keyword">void</span> <span class="hljs-title function_">destroyClient</span><span class="hljs-params">()</span>&#123;<br>        <span class="hljs-keyword">try</span> &#123;<br>            <span class="hljs-built_in">this</span>.client.close();<br>        &#125; <span class="hljs-keyword">catch</span> (IOException e) &#123;<br>            log.error(<span class="hljs-string">&quot;关闭资源出错,&#123;&#125;&quot;</span>,e.getMessage());<br>            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RuntimeException</span>(e);<br>        &#125;<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 创建索引库</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-meta">@Test</span><br>    <span class="hljs-keyword">void</span> <span class="hljs-title function_">createHotelIndexTest</span><span class="hljs-params">()</span>&#123;<br>        <span class="hljs-comment">// 1.创建Request对象</span><br>        <span class="hljs-type">CreateIndexRequest</span> <span class="hljs-variable">request</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">CreateIndexRequest</span>(<span class="hljs-string">&quot;hotel&quot;</span>);<br>        <span class="hljs-comment">// 2.请求参数，MAPPING_TEMPLATE是静态常量字符串，内容是创建索引库的DSL语句</span><br>        request.source(HotelContants.MAPPING_TEMPLATE, XContentType.JSON);<br>        <span class="hljs-comment">// 3.发起请求</span><br>        <span class="hljs-keyword">try</span> &#123;<br>            client.indices().create(request, RequestOptions.DEFAULT);<br>        &#125; <span class="hljs-keyword">catch</span> (IOException e) &#123;<br>            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RuntimeException</span>(e);<br>        &#125;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<blockquote>
<p>运行成功，前往kibana查询索引库</p>
</blockquote>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">GET</span> <span class="hljs-operator">/</span>hotel<br></code></pre></td></tr></table></figure>

<p><img src="https://gitee.com/tongstyle/blog-img/raw/master/img/202307132141049.png" srcset="/img/loading.gif" lazyload alt="image-20230525160904452"></p>
<blockquote>
<p>删除索引库、判断索引库是否存在</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> com.example.demo.constants.HotelContants;<br><span class="hljs-keyword">import</span> lombok.extern.slf4j.Slf4j;<br><span class="hljs-keyword">import</span> org.apache.http.HttpHost;<br><span class="hljs-keyword">import</span> org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;<br><span class="hljs-keyword">import</span> org.elasticsearch.client.RequestOptions;<br><span class="hljs-keyword">import</span> org.elasticsearch.client.RestClient;<br><span class="hljs-keyword">import</span> org.elasticsearch.client.RestHighLevelClient;<br><span class="hljs-keyword">import</span> org.elasticsearch.client.indices.CreateIndexRequest;<br><span class="hljs-keyword">import</span> org.elasticsearch.client.indices.GetIndexRequest;<br><span class="hljs-keyword">import</span> org.elasticsearch.common.xcontent.XContentType;<br><span class="hljs-keyword">import</span> org.junit.jupiter.api.AfterEach;<br><span class="hljs-keyword">import</span> org.junit.jupiter.api.BeforeEach;<br><span class="hljs-keyword">import</span> org.junit.jupiter.api.Test;<br><span class="hljs-keyword">import</span> org.springframework.boot.test.context.SpringBootTest;<br><br><span class="hljs-keyword">import</span> java.io.IOException;<br><br><span class="hljs-meta">@Slf4j</span><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">HotelDocumentTest</span> &#123;<br><br>    <span class="hljs-keyword">private</span> RestHighLevelClient client;<br><br>    <span class="hljs-meta">@BeforeEach</span><br>    <span class="hljs-keyword">void</span> <span class="hljs-title function_">initClient</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-built_in">this</span>.client = <span class="hljs-keyword">new</span> <span class="hljs-title class_">RestHighLevelClient</span>(RestClient.builder(<br>                HttpHost.create(<span class="hljs-string">&quot;http://localhost:9200&quot;</span>)<br>                <span class="hljs-comment">// 若是集群，可指定多个地址如下</span><br>                <span class="hljs-comment">//HttpHost.create(&quot;http://localhost:9200&quot;),</span><br>                <span class="hljs-comment">//HttpHost.create(&quot;http://localhost:9200&quot;)</span><br>        ));<br>    &#125;<br><br>    <span class="hljs-meta">@AfterEach</span><br>    <span class="hljs-keyword">void</span> <span class="hljs-title function_">destroyClient</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">try</span> &#123;<br>            <span class="hljs-built_in">this</span>.client.close();<br>        &#125; <span class="hljs-keyword">catch</span> (IOException e) &#123;<br>            log.error(<span class="hljs-string">&quot;关闭资源出错,&#123;&#125;&quot;</span>, e.getMessage());<br>            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RuntimeException</span>(e);<br>        &#125;<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 创建索引库</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-meta">@Test</span><br>    <span class="hljs-keyword">void</span> <span class="hljs-title function_">createHotelIndexTest</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-comment">// 1.创建Request对象</span><br>        <span class="hljs-type">CreateIndexRequest</span> <span class="hljs-variable">request</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">CreateIndexRequest</span>(<span class="hljs-string">&quot;hotel&quot;</span>);<br>        <span class="hljs-comment">// 2.请求参数，MAPPING_TEMPLATE是静态常量字符串，内容是创建索引库的DSL语句</span><br>        request.source(HotelContants.MAPPING_TEMPLATE, XContentType.JSON);<br>        <span class="hljs-comment">// 3.发起请求</span><br>        <span class="hljs-keyword">try</span> &#123;<br>            client.indices().create(request, RequestOptions.DEFAULT);<br>        &#125; <span class="hljs-keyword">catch</span> (IOException e) &#123;<br>            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RuntimeException</span>(e);<br>        &#125;<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 删除索引库</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-meta">@Test</span><br>    <span class="hljs-keyword">void</span> <span class="hljs-title function_">deleteHotelIndexTest</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-comment">// 1.创建Request对象</span><br>        <span class="hljs-type">DeleteIndexRequest</span> <span class="hljs-variable">request</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">DeleteIndexRequest</span>(<span class="hljs-string">&quot;hotel&quot;</span>);<br>        <span class="hljs-comment">// 2.发起请求</span><br>        <span class="hljs-keyword">try</span> &#123;<br>            client.indices().delete(request, RequestOptions.DEFAULT);<br>        &#125; <span class="hljs-keyword">catch</span> (IOException e) &#123;<br>            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RuntimeException</span>(e);<br>        &#125;<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 判断索引库是否存在</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-meta">@Test</span><br>    <span class="hljs-keyword">void</span> <span class="hljs-title function_">existsHotelIndexTest</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-comment">// 1.创建Request对象</span><br>        <span class="hljs-type">GetIndexRequest</span> <span class="hljs-variable">request</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">GetIndexRequest</span>(<span class="hljs-string">&quot;hotel&quot;</span>);<br>        <span class="hljs-comment">// 2.发起请求</span><br>        <span class="hljs-type">boolean</span> <span class="hljs-variable">exists</span> <span class="hljs-operator">=</span> <span class="hljs-literal">false</span>;<br>        <span class="hljs-keyword">try</span> &#123;<br>            exists = client.indices().exists(request, RequestOptions.DEFAULT);<br>        &#125; <span class="hljs-keyword">catch</span> (IOException e) &#123;<br>            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RuntimeException</span>(e);<br>        &#125;<br>        System.out.println(<span class="hljs-string">&quot;索引库&quot;</span>+(exists?<span class="hljs-string">&quot;存在&quot;</span>:<span class="hljs-string">&quot;不存在&quot;</span>));<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h3 id="索引库总结"><a href="#索引库总结" class="headerlink" title="索引库总结"></a>索引库总结</h3><p>索引库操作的基本步骤</p>
<ol>
<li>初始化RestHighLevelClient</li>
<li>创建XXXIndexRequest（XXX是create、get、delete）</li>
<li>准备DSL（create时需要语句）</li>
<li>发送请求，调用RestHighLevelClient对象的indices().xxx()方法（xxx是create、exists、delete）</li>
</ol>
<h2 id="文档操作"><a href="#文档操作" class="headerlink" title="文档操作"></a>文档操作</h2><h3 id="插入数据"><a href="#插入数据" class="headerlink" title="插入数据"></a>插入数据</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> com.alibaba.fastjson.JSON;<br><span class="hljs-keyword">import</span> com.example.demo.domain.Hotel;<br><span class="hljs-keyword">import</span> com.example.demo.domain.HotelDoc;<br><span class="hljs-keyword">import</span> com.example.demo.service.HotelService;<br><span class="hljs-keyword">import</span> lombok.extern.slf4j.Slf4j;<br><span class="hljs-keyword">import</span> org.apache.http.HttpHost;<br><span class="hljs-keyword">import</span> org.elasticsearch.action.index.IndexRequest;<br><span class="hljs-keyword">import</span> org.elasticsearch.client.RequestOptions;<br><span class="hljs-keyword">import</span> org.elasticsearch.client.RestClient;<br><span class="hljs-keyword">import</span> org.elasticsearch.client.RestHighLevelClient;<br><span class="hljs-keyword">import</span> org.elasticsearch.common.xcontent.XContentType;<br><span class="hljs-keyword">import</span> org.junit.jupiter.api.AfterEach;<br><span class="hljs-keyword">import</span> org.junit.jupiter.api.BeforeEach;<br><span class="hljs-keyword">import</span> org.junit.jupiter.api.Test;<br><span class="hljs-keyword">import</span> org.springframework.beans.factory.annotation.Autowired;<br><span class="hljs-keyword">import</span> org.springframework.boot.test.context.SpringBootTest;<br><br><span class="hljs-keyword">import</span> java.io.IOException;<br><br><span class="hljs-meta">@Slf4j</span><br><span class="hljs-meta">@SpringBootTest</span><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">HotelIndexTest</span> &#123;<br>    <span class="hljs-meta">@Autowired</span><br>    <span class="hljs-keyword">private</span> HotelService hotelService;<br>    <span class="hljs-keyword">private</span> RestHighLevelClient client;<br><br>    <span class="hljs-meta">@BeforeEach</span><br>    <span class="hljs-keyword">void</span> <span class="hljs-title function_">initClient</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-built_in">this</span>.client = <span class="hljs-keyword">new</span> <span class="hljs-title class_">RestHighLevelClient</span>(RestClient.builder(<br>                HttpHost.create(<span class="hljs-string">&quot;http://localhost:9200&quot;</span>)<br>                <span class="hljs-comment">// 若是集群，可指定多个地址如下</span><br>                <span class="hljs-comment">//HttpHost.create(&quot;http://localhost:9200&quot;),</span><br>                <span class="hljs-comment">//HttpHost.create(&quot;http://localhost:9200&quot;)</span><br>        ));<br>    &#125;<br><br>    <span class="hljs-meta">@AfterEach</span><br>    <span class="hljs-keyword">void</span> <span class="hljs-title function_">destroyClient</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">try</span> &#123;<br>            <span class="hljs-built_in">this</span>.client.close();<br>        &#125; <span class="hljs-keyword">catch</span> (IOException e) &#123;<br>            log.error(<span class="hljs-string">&quot;关闭资源出错,&#123;&#125;&quot;</span>, e.getMessage());<br>            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RuntimeException</span>(e);<br>        &#125;<br>    &#125;<br><br>    <span class="hljs-meta">@Test</span><br>    <span class="hljs-keyword">void</span> <span class="hljs-title function_">addIndexTest</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-comment">// hotel对象与索引库不符，经纬度是分开的两个字段，索引库则是一个字段</span><br>        <span class="hljs-comment">// 所以新建一个HotelDoc类与索引库对应</span><br>        <span class="hljs-type">Hotel</span> <span class="hljs-variable">hotel</span> <span class="hljs-operator">=</span> hotelService.getById(<span class="hljs-number">61083L</span>);<br>        <span class="hljs-type">HotelDoc</span> <span class="hljs-variable">hotelDoc</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">HotelDoc</span>(hotel);<br>        <span class="hljs-comment">// 1.准备request对象</span><br>        <span class="hljs-type">IndexRequest</span> <span class="hljs-variable">request</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">IndexRequest</span>(<span class="hljs-string">&quot;hotel&quot;</span>).id(hotel.getId().toString());<br>        <span class="hljs-comment">// 2.准备json文档</span><br>        request.source(JSON.toJSONString(hotelDoc), XContentType.JSON);<br>        <span class="hljs-comment">// 3.发送请求</span><br>        <span class="hljs-keyword">try</span> &#123;<br>            client.index(request, RequestOptions.DEFAULT);<br>        &#125; <span class="hljs-keyword">catch</span> (IOException e) &#123;<br>            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RuntimeException</span>(e);<br>        &#125;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> lombok.AllArgsConstructor;<br><span class="hljs-keyword">import</span> lombok.Data;<br><span class="hljs-keyword">import</span> lombok.NoArgsConstructor;<br><br><span class="hljs-meta">@Data</span><br><span class="hljs-meta">@AllArgsConstructor</span><br><span class="hljs-meta">@NoArgsConstructor</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">HotelDoc</span> &#123;<br>    <span class="hljs-keyword">private</span> Long id;<br>    <span class="hljs-keyword">private</span> String name;<br>    <span class="hljs-keyword">private</span> String address;<br>    <span class="hljs-keyword">private</span> Integer price;<br>    <span class="hljs-keyword">private</span> Integer score;<br>    <span class="hljs-keyword">private</span> String brand;<br>    <span class="hljs-keyword">private</span> String city;<br>    <span class="hljs-keyword">private</span> String starName;<br>    <span class="hljs-keyword">private</span> String business;<br>    <span class="hljs-keyword">private</span> String location;<br>    <span class="hljs-keyword">private</span> String pic;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-title function_">HotelDoc</span><span class="hljs-params">(Hotel hotel)</span> &#123;<br>        <span class="hljs-built_in">this</span>.id = hotel.getId();<br>        <span class="hljs-built_in">this</span>.name = hotel.getName();<br>        <span class="hljs-built_in">this</span>.address = hotel.getAddress();<br>        <span class="hljs-built_in">this</span>.price = hotel.getPrice();<br>        <span class="hljs-built_in">this</span>.score = hotel.getScore();<br>        <span class="hljs-built_in">this</span>.brand = hotel.getBrand();<br>        <span class="hljs-built_in">this</span>.city = hotel.getCity();<br>        <span class="hljs-built_in">this</span>.starName = hotel.getStarName();<br>        <span class="hljs-built_in">this</span>.business = hotel.getBusiness();<br>        <span class="hljs-built_in">this</span>.location = hotel.getLatitude() + <span class="hljs-string">&quot;,&quot;</span> + hotel.getLongitude();<br>        <span class="hljs-built_in">this</span>.pic = hotel.getPic();<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<blockquote>
<h3 id="这里运行报错了，不过不影响，插入数据已经成功"><a href="#这里运行报错了，不过不影响，插入数据已经成功" class="headerlink" title="这里运行报错了，不过不影响，插入数据已经成功"></a>这里运行报错了，不过不影响，插入数据已经成功</h3><p>kibana使用<code>GET /hotel/_doc/61083</code>查询</p>
<p><a target="_blank" rel="noopener" href="https://blog.csdn.net/qq_34263207/article/details/127788269">参考博客</a></p>
</blockquote>
<p><img src="https://gitee.com/tongstyle/blog-img/raw/master/img/202307132141135.png" srcset="/img/loading.gif" lazyload alt="image-20230525173745593"></p>
<p>查看日志打印，发现</p>
<p><img src="https://gitee.com/tongstyle/blog-img/raw/master/img/202307132141829.png" srcset="/img/loading.gif" lazyload alt="image-20230525174026003"></p>
<p>es版本&gt;8 推介查看<a target="_blank" rel="noopener" href="https://blog.csdn.net/qq_34263207/article/details/127847033">博客</a></p>
<blockquote>
<h1 id="添加依赖和配置"><a href="#添加依赖和配置" class="headerlink" title="添加依赖和配置"></a>添加依赖和配置</h1></blockquote>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>co.elastic.clients<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>elasticsearch-java<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>8.1.1<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>jakarta.json<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>jakarta.json-api<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>2.0.1<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br></code></pre></td></tr></table></figure>

<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs yaml"><span class="hljs-attr">spring:</span><br>  <span class="hljs-attr">es:</span><br>    <span class="hljs-attr">address:</span> <span class="hljs-string">localhost</span><br>    <span class="hljs-attr">port:</span> <span class="hljs-number">9200</span><br>    <span class="hljs-attr">scheme:</span> <span class="hljs-string">http</span><br>    <span class="hljs-attr">username:</span> <span class="hljs-string">elastic</span><br>    <span class="hljs-attr">password:</span> <span class="hljs-number">123456</span><br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch.ElasticsearchClient;<br><span class="hljs-keyword">import</span> co.elastic.clients.json.jackson.JacksonJsonpMapper;<br><span class="hljs-keyword">import</span> co.elastic.clients.transport.ElasticsearchTransport;<br><span class="hljs-keyword">import</span> co.elastic.clients.transport.rest_client.RestClientTransport;<br><span class="hljs-keyword">import</span> org.apache.http.HttpHost;<br><span class="hljs-keyword">import</span> org.apache.http.auth.AuthScope;<br><span class="hljs-keyword">import</span> org.apache.http.auth.UsernamePasswordCredentials;<br><span class="hljs-keyword">import</span> org.apache.http.client.CredentialsProvider;<br><span class="hljs-keyword">import</span> org.apache.http.impl.client.BasicCredentialsProvider;<br><span class="hljs-keyword">import</span> org.apache.http.impl.nio.client.HttpAsyncClientBuilder;<br><span class="hljs-keyword">import</span> org.elasticsearch.client.RestClient;<br><span class="hljs-keyword">import</span> org.elasticsearch.client.RestClientBuilder;<br><span class="hljs-keyword">import</span> org.springframework.beans.factory.annotation.Value;<br><span class="hljs-keyword">import</span> org.springframework.context.annotation.Bean;<br><span class="hljs-keyword">import</span> org.springframework.context.annotation.Configuration;<br><br><span class="hljs-meta">@Configuration</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">ElasticSearchConfig</span> &#123;<br><br>    <span class="hljs-meta">@Value(&quot;$&#123;spring.es.address&#125;&quot;)</span><br>    String address;<br><br>    <span class="hljs-meta">@Value(&quot;$&#123;spring.es.port&#125;&quot;)</span><br>    Integer port;<br><br>    <span class="hljs-meta">@Value(&quot;$&#123;spring.es.scheme&#125;&quot;)</span><br>    String scheme;<br><br>    <span class="hljs-meta">@Value(&quot;$&#123;spring.es.username&#125;&quot;)</span><br>    String username;<br><br>    <span class="hljs-meta">@Value(&quot;$&#123;spring.es.password&#125;&quot;)</span><br>    String password;<br><br>    <span class="hljs-meta">@Bean</span><br>    <span class="hljs-keyword">public</span> ElasticsearchClient <span class="hljs-title function_">esRestClientWithCred</span><span class="hljs-params">()</span>&#123;<br>        <span class="hljs-keyword">final</span> <span class="hljs-type">CredentialsProvider</span> <span class="hljs-variable">credentialsProvider</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">BasicCredentialsProvider</span>();<br>        <span class="hljs-comment">// 配置连接ES的用户名和密码，如果没有用户名和密码可以不加这一行</span><br>        credentialsProvider.setCredentials(AuthScope.ANY, <span class="hljs-keyword">new</span> <span class="hljs-title class_">UsernamePasswordCredentials</span>(username, password));<br>        <span class="hljs-type">RestClientBuilder</span> <span class="hljs-variable">restClientBuilder</span> <span class="hljs-operator">=</span> RestClient.builder(<span class="hljs-keyword">new</span> <span class="hljs-title class_">HttpHost</span>(address, port, scheme))<br>                .setHttpClientConfigCallback(<span class="hljs-keyword">new</span> <span class="hljs-title class_">RestClientBuilder</span>.HttpClientConfigCallback() &#123;<br>                    <span class="hljs-meta">@Override</span><br>                    <span class="hljs-keyword">public</span> HttpAsyncClientBuilder <span class="hljs-title function_">customizeHttpClient</span><span class="hljs-params">(HttpAsyncClientBuilder httpAsyncClientBuilder)</span> &#123;<br>                        <span class="hljs-keyword">return</span> httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider);<br>                    &#125;<br>                &#125;);<br>        <span class="hljs-type">RestClient</span> <span class="hljs-variable">restClient</span> <span class="hljs-operator">=</span> restClientBuilder.build();<br>        <span class="hljs-type">ElasticsearchTransport</span> <span class="hljs-variable">transport</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RestClientTransport</span>(<br>                restClient, <span class="hljs-keyword">new</span> <span class="hljs-title class_">JacksonJsonpMapper</span>());<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ElasticsearchClient</span>(transport);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>



<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch.ElasticsearchClient;<br><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch.core.GetResponse;<br><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch.core.IndexRequest;<br><span class="hljs-keyword">import</span> com.alibaba.fastjson.JSON;<br><span class="hljs-keyword">import</span> com.example.demo.domain.Hotel;<br><span class="hljs-keyword">import</span> com.example.demo.domain.HotelDoc;<br><span class="hljs-keyword">import</span> com.example.demo.service.HotelService;<br><span class="hljs-keyword">import</span> lombok.extern.slf4j.Slf4j;<br><span class="hljs-keyword">import</span> org.junit.jupiter.api.Test;<br><span class="hljs-keyword">import</span> org.springframework.beans.factory.annotation.Autowired;<br><span class="hljs-keyword">import</span> org.springframework.boot.test.context.SpringBootTest;<br><br><span class="hljs-keyword">import</span> java.io.IOException;<br><br><span class="hljs-meta">@Slf4j</span><br><span class="hljs-meta">@SpringBootTest</span><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">HotelIndexElasticTest</span> &#123;<br>    <span class="hljs-meta">@Autowired</span><br>    <span class="hljs-keyword">private</span> HotelService hotelService;<br>    <span class="hljs-meta">@Autowired</span><br>    ElasticsearchClient client;<br><br>    <span class="hljs-meta">@Test</span><br>    <span class="hljs-keyword">void</span> <span class="hljs-title function_">addIndexTest</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-comment">// 创建要插入的实体</span><br>        <span class="hljs-comment">// hotel对象与索引库不符，经纬度是分开的两个字段，索引库则是一个字段</span><br>        <span class="hljs-comment">// 所以新建一个HotelDoc类与索引库对应</span><br>        <span class="hljs-type">Hotel</span> <span class="hljs-variable">hotel</span> <span class="hljs-operator">=</span> hotelService.getById(<span class="hljs-number">61083</span>);<br>        <span class="hljs-type">HotelDoc</span> <span class="hljs-variable">hotelDoc</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">HotelDoc</span>(hotel);<br>        <span class="hljs-comment">// 方法一</span><br>        IndexRequest&lt;Object&gt; indexRequest = <span class="hljs-keyword">new</span> <span class="hljs-title class_">IndexRequest</span>.Builder&lt;&gt;()<br>                <span class="hljs-comment">// 博客中并未添加这句话，需要自己指定id，否则 GET /hotel/_doc/61083 在kibana中无法查询到相关数据</span><br>                .id(hotel.getId().toString())<br>                .index(<span class="hljs-string">&quot;hotel&quot;</span>)<br>                .document(hotelDoc)<br>                .build();<br>        <span class="hljs-keyword">try</span> &#123;<br>            client.index(indexRequest);<br>        &#125; <span class="hljs-keyword">catch</span> (IOException e) &#123;<br>            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RuntimeException</span>(e);<br>        &#125;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<blockquote>
<h3 id="在上述代码中遇到过一个问题"><a href="#在上述代码中遇到过一个问题" class="headerlink" title="在上述代码中遇到过一个问题"></a>在上述代码中遇到过一个<code>问题</code></h3><p>正常运行结束，但是在kibana中无法查询到数据</p>
<p>是因为没有手动指定id，具体查看上述代码注释</p>
</blockquote>
<blockquote>
<h4 id="采用lambda表达式的写法"><a href="#采用lambda表达式的写法" class="headerlink" title="采用lambda表达式的写法"></a>采用lambda表达式的写法</h4></blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch.ElasticsearchClient;<br><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch.core.GetResponse;<br><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch.core.IndexRequest;<br><span class="hljs-keyword">import</span> com.alibaba.fastjson.JSON;<br><span class="hljs-keyword">import</span> com.example.demo.domain.Hotel;<br><span class="hljs-keyword">import</span> com.example.demo.domain.HotelDoc;<br><span class="hljs-keyword">import</span> com.example.demo.service.HotelService;<br><span class="hljs-keyword">import</span> lombok.extern.slf4j.Slf4j;<br><span class="hljs-keyword">import</span> org.junit.jupiter.api.Test;<br><span class="hljs-keyword">import</span> org.springframework.beans.factory.annotation.Autowired;<br><span class="hljs-keyword">import</span> org.springframework.boot.test.context.SpringBootTest;<br><br><span class="hljs-keyword">import</span> java.io.IOException;<br><br><span class="hljs-meta">@Slf4j</span><br><span class="hljs-meta">@SpringBootTest</span><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">HotelIndexElasticTest</span> &#123;<br>    <span class="hljs-meta">@Autowired</span><br>    <span class="hljs-keyword">private</span> HotelService hotelService;<br>    <span class="hljs-meta">@Autowired</span><br>    ElasticsearchClient client;<br><br>    <span class="hljs-meta">@Test</span><br>    <span class="hljs-keyword">void</span> <span class="hljs-title function_">addIndexTest2</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-comment">// 创建要插入的实体</span><br>        <span class="hljs-comment">// hotel对象与索引库不符，经纬度是分开的两个字段，索引库则是一个字段</span><br>        <span class="hljs-comment">// 所以新建一个HotelDoc类与索引库对应</span><br>        <span class="hljs-type">Hotel</span> <span class="hljs-variable">hotel</span> <span class="hljs-operator">=</span> hotelService.getById(<span class="hljs-number">61083</span>);<br>        <span class="hljs-type">HotelDoc</span> <span class="hljs-variable">hotelDoc</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">HotelDoc</span>(hotel);<br>        <span class="hljs-comment">// 方法二</span><br>        <span class="hljs-keyword">try</span> &#123;<br>            client.index(item -&gt; item<br>                    .id(hotel.getId().toString())<br>                    .index(<span class="hljs-string">&quot;hotel&quot;</span>)<br>                    .document(hotelDoc));<br>        &#125; <span class="hljs-keyword">catch</span> (IOException e) &#123;<br>            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RuntimeException</span>(e);<br>        &#125;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h3 id="查询数据"><a href="#查询数据" class="headerlink" title="查询数据"></a>查询数据</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch.ElasticsearchClient;<br><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch.core.GetResponse;<br><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch.core.IndexRequest;<br><span class="hljs-keyword">import</span> com.alibaba.fastjson.JSON;<br><span class="hljs-keyword">import</span> com.example.demo.domain.Hotel;<br><span class="hljs-keyword">import</span> com.example.demo.domain.HotelDoc;<br><span class="hljs-keyword">import</span> com.example.demo.service.HotelService;<br><span class="hljs-keyword">import</span> lombok.extern.slf4j.Slf4j;<br><span class="hljs-keyword">import</span> org.junit.jupiter.api.Test;<br><span class="hljs-keyword">import</span> org.springframework.beans.factory.annotation.Autowired;<br><span class="hljs-keyword">import</span> org.springframework.boot.test.context.SpringBootTest;<br><br><span class="hljs-keyword">import</span> java.io.IOException;<br><br><span class="hljs-meta">@Slf4j</span><br><span class="hljs-meta">@SpringBootTest</span><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">HotelIndexElasticTest</span> &#123;<br>    <span class="hljs-meta">@Autowired</span><br>    <span class="hljs-keyword">private</span> HotelService hotelService;<br>    <span class="hljs-meta">@Autowired</span><br>    ElasticsearchClient client;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 获取索引</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-meta">@Test</span><br>    <span class="hljs-keyword">void</span> <span class="hljs-title function_">getIndexTest</span><span class="hljs-params">()</span>&#123;<br>        <span class="hljs-type">String</span> <span class="hljs-variable">id</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;61083&quot;</span>;<br>        GetResponse&lt;HotelDoc&gt; response = <span class="hljs-literal">null</span>;<br>        <span class="hljs-keyword">try</span> &#123;<br>            response = client.get(g -&gt; g<br>                            .index(<span class="hljs-string">&quot;hotel&quot;</span>)<br>                            .id(id),<br>                    HotelDoc.class<br>            );<br>        &#125; <span class="hljs-keyword">catch</span> (IOException e) &#123;<br>            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RuntimeException</span>(e);<br>        &#125;<br><br>        <span class="hljs-keyword">if</span> (response.found()) &#123;<br>            <span class="hljs-type">HotelDoc</span> <span class="hljs-variable">hotelDoc</span> <span class="hljs-operator">=</span> response.source();<br>            log.info(<span class="hljs-string">&quot;返回结果 &quot;</span> + JSON.toJSONString(hotelDoc));<br>        &#125; <span class="hljs-keyword">else</span> &#123;<br>            log.info(<span class="hljs-string">&quot;数据不存在&quot;</span>);<br>        &#125;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h3 id="修改数据"><a href="#修改数据" class="headerlink" title="修改数据"></a>修改数据</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch.ElasticsearchClient;<br><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch.core.GetResponse;<br><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch.core.IndexRequest;<br><span class="hljs-keyword">import</span> com.alibaba.fastjson.JSON;<br><span class="hljs-keyword">import</span> com.example.demo.domain.Hotel;<br><span class="hljs-keyword">import</span> com.example.demo.domain.HotelDoc;<br><span class="hljs-keyword">import</span> com.example.demo.service.HotelService;<br><span class="hljs-keyword">import</span> lombok.extern.slf4j.Slf4j;<br><span class="hljs-keyword">import</span> org.junit.jupiter.api.Test;<br><span class="hljs-keyword">import</span> org.springframework.beans.factory.annotation.Autowired;<br><span class="hljs-keyword">import</span> org.springframework.boot.test.context.SpringBootTest;<br><br><span class="hljs-keyword">import</span> java.io.IOException;<br><br><span class="hljs-meta">@Slf4j</span><br><span class="hljs-meta">@SpringBootTest</span><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">HotelIndexElasticTest</span> &#123;<br>    <span class="hljs-meta">@Autowired</span><br>    <span class="hljs-keyword">private</span> HotelService hotelService;<br>    <span class="hljs-meta">@Autowired</span><br>    ElasticsearchClient client;<br>    <br>    <span class="hljs-meta">@Test</span><br>    <span class="hljs-keyword">void</span> <span class="hljs-title function_">updateIndexTest</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-type">Hotel</span> <span class="hljs-variable">hotel</span> <span class="hljs-operator">=</span> hotelService.getById(<span class="hljs-number">61083</span>);<br>        <span class="hljs-type">HotelDoc</span> <span class="hljs-variable">hotelDoc</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">HotelDoc</span>(hotel);<br>        <span class="hljs-comment">// 将城市从“上海”变为“不在上海”</span><br>        hotelDoc.setCity(<span class="hljs-string">&quot;不在上海&quot;</span>);<br>        <span class="hljs-keyword">try</span> &#123;<br>            client.update(item -&gt; item<br>                            .index(<span class="hljs-string">&quot;hotel&quot;</span>)<br>                            .doc(hotelDoc)<br>                            .id(hotel.getId().toString()),<br>                    HotelDoc.class<br>            );<br>        &#125; <span class="hljs-keyword">catch</span> (IOException e) &#123;<br>            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RuntimeException</span>(e);<br>        &#125;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p><img src="https://gitee.com/tongstyle/blog-img/raw/master/img/202307132141653.png" srcset="/img/loading.gif" lazyload alt="image-20230525183341843"></p>
<h3 id="删除数据"><a href="#删除数据" class="headerlink" title="删除数据"></a>删除数据</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch.ElasticsearchClient;<br><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch.core.GetResponse;<br><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch.core.IndexRequest;<br><span class="hljs-keyword">import</span> com.alibaba.fastjson.JSON;<br><span class="hljs-keyword">import</span> com.example.demo.domain.Hotel;<br><span class="hljs-keyword">import</span> com.example.demo.domain.HotelDoc;<br><span class="hljs-keyword">import</span> com.example.demo.service.HotelService;<br><span class="hljs-keyword">import</span> lombok.extern.slf4j.Slf4j;<br><span class="hljs-keyword">import</span> org.junit.jupiter.api.Test;<br><span class="hljs-keyword">import</span> org.springframework.beans.factory.annotation.Autowired;<br><span class="hljs-keyword">import</span> org.springframework.boot.test.context.SpringBootTest;<br><br><span class="hljs-keyword">import</span> java.io.IOException;<br><br><span class="hljs-meta">@Slf4j</span><br><span class="hljs-meta">@SpringBootTest</span><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">HotelIndexElasticTest</span> &#123;<br>    <span class="hljs-meta">@Autowired</span><br>    <span class="hljs-keyword">private</span> HotelService hotelService;<br>    <span class="hljs-meta">@Autowired</span><br>    ElasticsearchClient client;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 删除索引测试</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-meta">@Test</span><br>    <span class="hljs-keyword">void</span> <span class="hljs-title function_">deleteIndexTest</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-type">Hotel</span> <span class="hljs-variable">hotel</span> <span class="hljs-operator">=</span> hotelService.getById(<span class="hljs-number">61083</span>);<br>        <span class="hljs-keyword">try</span> &#123;<br>            client.delete(item -&gt; item<br>                    .id(hotel.getId().toString())<br>                    .index(<span class="hljs-string">&quot;hotel&quot;</span>));<br>        &#125; <span class="hljs-keyword">catch</span> (IOException e) &#123;<br>            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RuntimeException</span>(e);<br>        &#125;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p><img src="https://gitee.com/tongstyle/blog-img/raw/master/img/202307132142573.png" srcset="/img/loading.gif" lazyload alt="image-20230525183714877"></p>
<h3 id="批量导入数据"><a href="#批量导入数据" class="headerlink" title="批量导入数据"></a>批量导入数据</h3><blockquote>
<h1 id="利用JavaRestClient批量导入酒店数据到ES"><a href="#利用JavaRestClient批量导入酒店数据到ES" class="headerlink" title="利用JavaRestClient批量导入酒店数据到ES"></a>利用JavaRestClient批量导入酒店数据到ES</h1><p>需求:批量查询酒店数据，然后批量导入索引库中</p>
<p>思路:</p>
<ol>
<li>利用mybatis-plus查询酒店数据</li>
<li>将查询到的酒店数据(Hotel) 转换为文档类型数据( HotelDoc )</li>
<li>利用JavaRestClient中 的Bulk批处理，实现批量新增文档</li>
</ol>
</blockquote>
<img src="https://gitee.com/tongstyle/blog-img/raw/master/img/202307132142688.png" srcset="/img/loading.gif" lazyload alt="image-20230525211027361" style="zoom:80%;" />

<blockquote>
<h3 id="黑马程序员里的写法如上图所示"><a href="#黑马程序员里的写法如上图所示" class="headerlink" title="黑马程序员里的写法如上图所示"></a>黑马程序员里的写法如上图所示</h3><p>但是由于我在windows安装的elasticsearch的版本问题，所以采用如下写法</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch.ElasticsearchClient;<br><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch.core.BulkRequest;<br><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch.core.GetResponse;<br><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch.core.IndexRequest;<br><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch.core.bulk.BulkOperation;<br><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch.core.bulk.IndexOperation;<br><span class="hljs-keyword">import</span> com.alibaba.fastjson.JSON;<br><span class="hljs-keyword">import</span> com.example.demo.domain.Hotel;<br><span class="hljs-keyword">import</span> com.example.demo.domain.HotelDoc;<br><span class="hljs-keyword">import</span> com.example.demo.service.HotelService;<br><span class="hljs-keyword">import</span> lombok.extern.slf4j.Slf4j;<br><span class="hljs-keyword">import</span> org.junit.jupiter.api.Test;<br><span class="hljs-keyword">import</span> org.springframework.beans.factory.annotation.Autowired;<br><span class="hljs-keyword">import</span> org.springframework.boot.test.context.SpringBootTest;<br><br><span class="hljs-keyword">import</span> java.io.IOException;<br><span class="hljs-keyword">import</span> java.util.ArrayList;<br><span class="hljs-keyword">import</span> java.util.List;<br><br><span class="hljs-meta">@Slf4j</span><br><span class="hljs-meta">@SpringBootTest</span><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">HotelIndexElasticTest</span> &#123;<br>    <span class="hljs-meta">@Autowired</span><br>    <span class="hljs-keyword">private</span> HotelService hotelService;<br>    <span class="hljs-meta">@Autowired</span><br>    ElasticsearchClient client;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 批量导入数据测试</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-meta">@Test</span><br>    <span class="hljs-keyword">void</span> <span class="hljs-title function_">bulkIndexListTest</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-comment">// 1.创建Bulk请求</span><br>        <span class="hljs-type">BulkRequest</span> <span class="hljs-variable">bulkRequest</span> <span class="hljs-operator">=</span> <span class="hljs-literal">null</span>;<br>        List&lt;BulkOperation&gt; bulkOperations = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayList</span>&lt;&gt;();<br>        <span class="hljs-keyword">for</span> (Hotel hotel : hotelService.list()) &#123;<br>            <span class="hljs-type">HotelDoc</span> <span class="hljs-variable">hotelDoc</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">HotelDoc</span>(hotel);<br>            IndexOperation&lt;Object&gt; build = <span class="hljs-keyword">new</span> <span class="hljs-title class_">IndexOperation</span>.Builder&lt;&gt;()<br>                    .id(hotelDoc.getId().toString())<br>                    .index(<span class="hljs-string">&quot;hotel&quot;</span>)<br>                    .document(hotelDoc).build();<br>            bulkOperations.add(build._toBulkOperation());<br>        &#125;<br>        <span class="hljs-comment">// 2.添加要批量提交的请求</span><br>        bulkRequest = <span class="hljs-keyword">new</span> <span class="hljs-title class_">BulkRequest</span>.Builder()<br>                .index(<span class="hljs-string">&quot;hotel&quot;</span>)<br>                .operations(bulkOperations)<br>                .build();<br>        System.out.printf(<span class="hljs-string">&quot;一共插入了%s条数据%n&quot;</span>,bulkRequest.operations().size());<br>        <span class="hljs-comment">// 3.发起bulk请求</span><br>        <span class="hljs-keyword">try</span> &#123;<br>            client.bulk(bulkRequest);<br>        &#125; <span class="hljs-keyword">catch</span> (IOException e) &#123;<br>            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RuntimeException</span>(e);<br>        &#125;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<blockquote>
<h1 id="批量查询"><a href="#批量查询" class="headerlink" title="批量查询"></a>批量查询</h1></blockquote>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">GET</span> <span class="hljs-operator">/</span>hotel<span class="hljs-operator">/</span>_search<br></code></pre></td></tr></table></figure>

<h3 id="文档总结"><a href="#文档总结" class="headerlink" title="文档总结"></a>文档总结</h3><p>文档操作的基本步骤</p>
<ol>
<li>初始化RestHighLevelClient</li>
<li>创建XXXRequest。（XXX是Index、Get、Update、Delete）</li>
<li>准备参数（Index和Update时需要）</li>
<li>发送请求，调用RestHighLevelClient对象的xxx()方法（xxx是index、get、update、delete）</li>
<li>解析结果（get时需要）</li>
</ol>
<h2 id="DSL查询及案例"><a href="#DSL查询及案例" class="headerlink" title="DSL查询及案例"></a>DSL查询及案例</h2><h3 id="DSL查询语法"><a href="#DSL查询语法" class="headerlink" title="DSL查询语法"></a>DSL查询语法</h3><h4 id="基本语法"><a href="#基本语法" class="headerlink" title="基本语法"></a>基本语法</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs sql"># 查询所有<br><span class="hljs-keyword">GET</span> <span class="hljs-operator">/</span>hotel<span class="hljs-operator">/</span>_search<br>&#123;<br>  &quot;query&quot;:&#123;<br>    &quot;match_all&quot;: &#123;&#125;<br>  &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h4 id="全文检索查询"><a href="#全文检索查询" class="headerlink" title="全文检索查询"></a>全文检索查询</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs sql"># <span class="hljs-keyword">match</span>查询<br><span class="hljs-keyword">GET</span> <span class="hljs-operator">/</span>hotel<span class="hljs-operator">/</span>_search<br>&#123;<br>  &quot;query&quot;:&#123;<br>    &quot;match&quot;:&#123;<br>      &quot;all&quot;:&quot;外滩&quot;<br>    &#125;<br>  &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<blockquote>
<h2 id="如下查询与上面的查询效果一样，但是由于检索多个字段，效率更低"><a href="#如下查询与上面的查询效果一样，但是由于检索多个字段，效率更低" class="headerlink" title="如下查询与上面的查询效果一样，但是由于检索多个字段，效率更低"></a>如下查询与上面的查询效果一样，但是由于检索多个字段，效率更低</h2><p>推介使用上面的查询<code>（copy_to）</code></p>
</blockquote>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs sql"># multi_match查询<br><span class="hljs-keyword">GET</span> <span class="hljs-operator">/</span>hotel<span class="hljs-operator">/</span>_search<br>&#123;<br>  &quot;query&quot;:&#123;<br>    &quot;multi_match&quot;: &#123;<br>      &quot;query&quot;: &quot;外滩如家&quot;,<br>      &quot;fields&quot;: [&quot;brand&quot;,&quot;name&quot;,&quot;business&quot;]<br>    &#125;<br>  &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h4 id="精确查询"><a href="#精确查询" class="headerlink" title="精确查询"></a>精确查询</h4><blockquote>
<h2 id="精确匹配不会对输入的内容进行分词，必须完全一致"><a href="#精确匹配不会对输入的内容进行分词，必须完全一致" class="headerlink" title="精确匹配不会对输入的内容进行分词，必须完全一致"></a>精确匹配不会对输入的内容进行分词，<code>必须完全一致</code></h2><p>假如:输入的是<code>上海杭州</code>，则查询结果为0</p>
</blockquote>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs sql"># term查询<br><span class="hljs-keyword">GET</span> <span class="hljs-operator">/</span>hotel<span class="hljs-operator">/</span>_search<br>&#123;<br>  &quot;query&quot;:&#123;<br>    &quot;term&quot;:&#123;<br>      &quot;city&quot;:&#123;<br>        &quot;value&quot;:&quot;上海&quot;<br>      &#125;<br>    &#125;<br>  &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<blockquote>
<h2 id="价格大于等于100，小于等于300"><a href="#价格大于等于100，小于等于300" class="headerlink" title="价格大于等于100，小于等于300"></a>价格大于等于100，小于等于300</h2><p><code>gt</code>为大于，<code>gte</code>为大于等于；<code>lte和lt同理</code></p>
</blockquote>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs sql"># <span class="hljs-keyword">range</span>查询<br><span class="hljs-keyword">GET</span> <span class="hljs-operator">/</span>hotel<span class="hljs-operator">/</span>_search<br>&#123;<br>  &quot;query&quot;:&#123;<br>    &quot;range&quot;:&#123;<br>      &quot;price&quot;: &#123;<br>        &quot;gte&quot;: <span class="hljs-number">100</span>,<br>        &quot;lte&quot;: <span class="hljs-number">300</span><br>      &#125;<br>    &#125;<br>  &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h4 id="地理查询"><a href="#地理查询" class="headerlink" title="地理查询"></a>地理查询</h4><blockquote>
<h2 id="查询在一个矩形范围内的地点"><a href="#查询在一个矩形范围内的地点" class="headerlink" title="查询在一个矩形范围内的地点"></a>查询在一个矩形范围内的地点</h2><p><code>top_left(左上角)</code>和<code>bottom_right(右下角)</code>分别确定一个点，两个点水平作两条直线得到的<code>矩形范围</code></p>
</blockquote>
<img src="https://gitee.com/tongstyle/blog-img/raw/master/img/202307132142348.png" srcset="/img/loading.gif" lazyload alt="image-20230525222759408" style="zoom: 67%;" />

<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><code class="hljs sql"># geo_bounding_box查询<br><span class="hljs-keyword">GET</span> <span class="hljs-operator">/</span>hotel<span class="hljs-operator">/</span>_search<br>&#123;<br>  &quot;query&quot;:&#123;<br>    &quot;geo_bounding_box&quot;: &#123;<br>      &quot;location&quot;: &#123;<br>        &quot;top_left&quot;: &#123;<br>          &quot;lat&quot;: <span class="hljs-number">31.1</span>,<br>          &quot;lon&quot;: <span class="hljs-number">121.5</span><br>        &#125;,<br>        &quot;bottom_right&quot;: &#123;<br>          &quot;lat&quot;: <span class="hljs-number">30.9</span>,<br>          &quot;lon&quot;: <span class="hljs-number">121.7</span><br>        &#125;<br>      &#125;<br>    &#125;<br>  &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<blockquote>
<h2 id="查询距离一个点一定范围内的数据"><a href="#查询距离一个点一定范围内的数据" class="headerlink" title="查询距离一个点一定范围内的数据"></a>查询距离一个<code>点</code>一定范围内的数据</h2></blockquote>
<img src="https://gitee.com/tongstyle/blog-img/raw/master/img/202307132142284.png" srcset="/img/loading.gif" lazyload alt="image-20230525222958773" style="zoom:67%;" />

<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs sql"># geo_distance查询<br><span class="hljs-keyword">GET</span> <span class="hljs-operator">/</span>hotel<span class="hljs-operator">/</span>_search<br>&#123;<br>  &quot;query&quot;:&#123;<br>    &quot;geo_distance&quot;: &#123;<br>      &quot;distance&quot;: &quot;15km&quot;,<br>      &quot;location&quot;: &#123;<br>        &quot;lat&quot;: <span class="hljs-number">31.21</span>,<br>        &quot;lon&quot;: <span class="hljs-number">121.5</span><br>      &#125;<br>    &#125;<br>  &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<blockquote>
<p>也可以使用下面这种写法</p>
</blockquote>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs sql"># geo_distance查询<br><span class="hljs-keyword">GET</span> <span class="hljs-operator">/</span>hotel<span class="hljs-operator">/</span>_search<br>&#123;<br>  &quot;query&quot;:&#123;<br>    &quot;geo_distance&quot;: &#123;<br>      &quot;distance&quot;: &quot;15km&quot;,<br>      &quot;location&quot;: &quot;31.21,121.5&quot;<br>    &#125;<br>  &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h4 id="Function-score-query"><a href="#Function-score-query" class="headerlink" title="Function score query"></a>Function score query</h4><p><img src="https://gitee.com/tongstyle/blog-img/raw/master/img/202307132142645.png" srcset="/img/loading.gif" lazyload alt="image-20230525224121984"></p>
<blockquote>
<h1 id="案例"><a href="#案例" class="headerlink" title="案例"></a>案例</h1><p>让“如家”这个品牌的酒店排名靠前一些</p>
</blockquote>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><code class="hljs sql"># 让“如家”这个品牌的酒店排名靠前一些<br># <span class="hljs-keyword">function</span> score查询<br><span class="hljs-keyword">GET</span> <span class="hljs-operator">/</span>hotel<span class="hljs-operator">/</span>_search<br>&#123;<br>  &quot;query&quot;:&#123;<br>    &quot;function_score&quot;: &#123;<br>      &quot;query&quot;: &#123;<br>        &quot;match&quot;: &#123;<br>          &quot;all&quot;: &quot;如家&quot;<br>        &#125;<br>      &#125;,<br>      &quot;functions&quot;: [<br>        &#123;<br>          &quot;filter&quot;:&#123;<br>            &quot;term&quot;:&#123;<br>              &quot;brand&quot;:&quot;如家&quot;<br>            &#125;<br>          &#125;,<br>          &quot;weight&quot;: <span class="hljs-number">10</span><br>        &#125;<br>      ],<br>      &quot;boost_mode&quot;: &quot;sum&quot;<br>    &#125;<br>  &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h4 id="复合查询Boolean-Query"><a href="#复合查询Boolean-Query" class="headerlink" title="复合查询Boolean Query"></a>复合查询Boolean Query</h4><blockquote>
<p>布尔查询是-个或多个查询子句的组合。子查询的组合方式有:</p>
<ol>
<li>must:必须匹配每个子查询，类似“与”</li>
<li>should:选择性匹配子查询，类似“或”</li>
<li>must_ not:必须不匹配，<code>不参与算分</code>，类似“非”</li>
<li>filter:必须匹配，<code>不参与算分</code></li>
</ol>
</blockquote>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><code class="hljs sql"># city在上海、brand是“皇冠假日”和“华美达”中的一个、price不能小于等于<span class="hljs-number">500</span>、score大于等于<span class="hljs-number">45</span><br><span class="hljs-keyword">GET</span> <span class="hljs-operator">/</span>hotel<span class="hljs-operator">/</span>_search<br>&#123;<br>  &quot;query&quot;:&#123;<br>    &quot;bool&quot;: &#123;<br>      &quot;must&quot;: [<br>        &#123;<br>          &quot;term&quot;: &#123;<br>            &quot;city&quot;: &#123;<br>              &quot;value&quot;: &quot;上海&quot;<br>            &#125;<br>          &#125;<br>        &#125;<br>      ],<br>      &quot;should&quot;: [<br>        &#123;<br>          &quot;term&quot;: &#123;<br>            &quot;brand&quot;: &#123;<br>              &quot;value&quot;: &quot;皇冠假日&quot;<br>            &#125;<br>          &#125;<br>        &#125;,<br>        &#123;<br>          &quot;term&quot;: &#123;<br>            &quot;brand&quot;: &#123;<br>              &quot;value&quot;: &quot;华美达&quot;<br>            &#125;<br>          &#125;<br>        &#125;<br>      ],<br>      &quot;must_not&quot;: [<br>        &#123;<br>          &quot;range&quot;: &#123;<br>            &quot;price&quot;: &#123;<br>              &quot;lte&quot;: <span class="hljs-number">500</span><br>            &#125;<br>          &#125;<br>        &#125;,<br>        &#123;<br>          &quot;range&quot;: &#123;<br>            &quot;score&quot;: &#123;<br>              &quot;gte&quot;: <span class="hljs-number">45</span><br>            &#125;<br>          &#125;<br>        &#125;<br>      ]<br>    &#125;<br>  &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<blockquote>
<h1 id="案例练习"><a href="#案例练习" class="headerlink" title="案例练习"></a>案例练习</h1><p>需求：搜索名字包含<code>如家</code>，价格不高于400，在坐标31.21、121.5周围10km范围内的酒店</p>
</blockquote>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><code class="hljs sql"># 搜索名字包含`如家`，价格不高于<span class="hljs-number">400</span>，在坐标<span class="hljs-number">31.21</span>、<span class="hljs-number">121.5</span>周围<span class="hljs-number">10</span>km范围内的酒店<br><span class="hljs-keyword">GET</span> <span class="hljs-operator">/</span>hotel<span class="hljs-operator">/</span>_search<br>&#123;<br>  &quot;query&quot;:&#123;<br>    &quot;bool&quot;: &#123;<br>      &quot;must&quot;: [<br>        &#123;<br>          &quot;match&quot;: &#123;<br>            &quot;name&quot;: &quot;如家&quot;<br>          &#125;<br>        &#125;<br>      ],<br>      &quot;must_not&quot;: [<br>        &#123;<br>          &quot;range&quot;: &#123;<br>            &quot;price&quot;: &#123;<br>              &quot;gt&quot;: <span class="hljs-number">400</span><br>            &#125;<br>          &#125;<br>        &#125;<br>      ],<br>      &quot;filter&quot;: [<br>        &#123;<br>          &quot;geo_distance&quot;: &#123;<br>            &quot;distance&quot;: &quot;10km&quot;,<br>            &quot;location&quot;: &#123;<br>              &quot;lat&quot;: <span class="hljs-number">31.21</span>,<br>              &quot;lon&quot;: <span class="hljs-number">121.5</span><br>            &#125;<br>          &#125;<br>        &#125;<br>      ]<br>    &#125;<br>  &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h3 id="搜索结果处理"><a href="#搜索结果处理" class="headerlink" title="搜索结果处理"></a>搜索结果处理</h3><h4 id="排序"><a href="#排序" class="headerlink" title="排序"></a>排序</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs sql"># 排序，排序字段和排序方式 <span class="hljs-keyword">asc</span>、<span class="hljs-keyword">desc</span><br><span class="hljs-keyword">GET</span> <span class="hljs-operator">/</span>hotel<span class="hljs-operator">/</span>_search<br>&#123;<br>  &quot;query&quot;:&#123;<br>    &quot;match_all&quot;: &#123;&#125;<br>  &#125;,<br>  &quot;sort&quot;:&#123;<br>    &quot;price&quot;:&quot;asc&quot;<br>  &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<blockquote>
<h1 id="地理位置排序"><a href="#地理位置排序" class="headerlink" title="地理位置排序"></a>地理位置排序</h1></blockquote>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><code class="hljs sql"># 排序，排序字段和排序方式 <span class="hljs-keyword">asc</span>、<span class="hljs-keyword">desc</span><br><span class="hljs-keyword">GET</span> <span class="hljs-operator">/</span>hotel<span class="hljs-operator">/</span>_search<br>&#123;<br>  &quot;query&quot;:&#123;<br>    &quot;match_all&quot;: &#123;&#125;<br>  &#125;,<br>  &quot;sort&quot;:&#123;<br>    &quot;_geo_distance&quot;:&#123;<br>      &quot;location&quot;:&#123;<br>        &quot;lat&quot;: <span class="hljs-number">31.21</span>,<br>        &quot;lon&quot;: <span class="hljs-number">121.5</span><br>      &#125;,<br>      &quot;order&quot;:&quot;asc&quot;,<br>      &quot;unit&quot;:&quot;km&quot;<br>    &#125;<br>  &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<blockquote>
<p>也可以采用如下写法</p>
</blockquote>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs sql"># 排序，排序字段和排序方式 <span class="hljs-keyword">asc</span>、<span class="hljs-keyword">desc</span><br><span class="hljs-keyword">GET</span> <span class="hljs-operator">/</span>hotel<span class="hljs-operator">/</span>_search<br>&#123;<br>  &quot;query&quot;:&#123;<br>    &quot;match_all&quot;: &#123;&#125;<br>  &#125;,<br>  &quot;sort&quot;:&#123;<br>    &quot;_geo_distance&quot;:&#123;<br>      &quot;location&quot;:&quot;31.21,121.5&quot;,<br>      &quot;order&quot;:&quot;asc&quot;,<br>      &quot;unit&quot;:&quot;km&quot;<br>    &#125;<br>  &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<blockquote>
<h1 id="案例：对酒店数据按照用户评价降序排序，评价相同的按照价格升序排序"><a href="#案例：对酒店数据按照用户评价降序排序，评价相同的按照价格升序排序" class="headerlink" title="案例：对酒店数据按照用户评价降序排序，评价相同的按照价格升序排序"></a>案例：对酒店数据按照用户评价降序排序，评价相同的按照价格升序排序</h1></blockquote>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs sql"># 对酒店数据按照用户评价降序排序，评价相同的按照价格升序排序<br><span class="hljs-keyword">GET</span> <span class="hljs-operator">/</span>hotel<span class="hljs-operator">/</span>_search<br>&#123;<br>  &quot;query&quot;:&#123;<br>    &quot;match_all&quot;: &#123;&#125;<br>  &#125;,<br>  &quot;sort&quot;:[<br>    &#123;<br>      &quot;score&quot;:&quot;desc&quot;<br>    &#125;,<br>    &#123;<br>      &quot;price&quot;:&quot;asc&quot;<br>    &#125;<br>  ]<br>&#125;<br></code></pre></td></tr></table></figure>

<blockquote>
<h1 id="案例：实现对酒店数据按照位置坐标的距离升序排序"><a href="#案例：实现对酒店数据按照位置坐标的距离升序排序" class="headerlink" title="案例：实现对酒店数据按照位置坐标的距离升序排序"></a>案例：实现对酒店数据按照位置坐标的距离升序排序</h1></blockquote>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><code class="hljs sql"># 实现对酒店数据按照位置坐标的距离升序排序<br><span class="hljs-keyword">GET</span> <span class="hljs-operator">/</span>hotel<span class="hljs-operator">/</span>_search<br>&#123;<br>  &quot;query&quot;:&#123;<br>    &quot;match_all&quot;: &#123;&#125;<br>  &#125;,<br>  &quot;sort&quot;:&#123;<br>    &quot;_geo_distance&quot;:&#123;<br>      &quot;location&quot;:&quot;31.21,121.5&quot;,<br>      &quot;order&quot;:&quot;asc&quot;,<br>      &quot;unit&quot;:&quot;km&quot;<br>    &#125;<br>  &#125;<br>&#125;<br><br># 或者采用如下地理位置写法<br># 实现对酒店数据按照位置坐标的距离升序排序<br><span class="hljs-keyword">GET</span> <span class="hljs-operator">/</span>hotel<span class="hljs-operator">/</span>_search<br>&#123;<br>  &quot;query&quot;:&#123;<br>    &quot;match_all&quot;: &#123;&#125;<br>  &#125;,<br>  &quot;sort&quot;:&#123;<br>    &quot;_geo_distance&quot;:&#123;<br>      &quot;location&quot;:&#123;<br>        &quot;lat&quot;:<span class="hljs-number">31.21</span>,<br>        &quot;lon&quot;:<span class="hljs-number">121.5</span><br>      &#125;,<br>      &quot;order&quot;:&quot;asc&quot;,<br>      &quot;unit&quot;:&quot;km&quot;<br>    &#125;<br>  &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h4 id="分页"><a href="#分页" class="headerlink" title="分页"></a>分页</h4><blockquote>
<p>elasticsearch默认情况下只返回top10的数据。而如果要查询更多数据就需要修改分页参数了。</p>
<p>elasticsearch中通过修改<code>from</code>、<code>size</code>参 数来控制要返回的分页结果</p>
</blockquote>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs sql"># 分页查询<br><span class="hljs-keyword">GET</span> <span class="hljs-operator">/</span>hotel<span class="hljs-operator">/</span>_search<br>&#123;<br>  &quot;query&quot;:&#123;<br>    &quot;match_all&quot;: &#123;&#125;<br>  &#125;,<br>  &quot;sort&quot;:[<br>    &#123;<br>      &quot;price&quot;:&quot;asc&quot;<br>    &#125;<br>  ],<br>  &quot;from&quot;:<span class="hljs-number">0</span>,<br>  &quot;size&quot;:<span class="hljs-number">5</span><br>&#125;<br></code></pre></td></tr></table></figure>

<blockquote>
<h2 id="深度分页问题"><a href="#深度分页问题" class="headerlink" title="深度分页问题"></a><code>深度分页问题</code></h2><p>ES是分布式的，所以会面临深度分页问题。例如按price排序后，获取from &#x3D; 990，size &#x3D;10的数据:</p>
</blockquote>
<img src="https://gitee.com/tongstyle/blog-img/raw/master/img/202307132142794.png" srcset="/img/loading.gif" lazyload alt="image-20230526095625293" style="zoom:67%;" />

<blockquote>
<h4 id="如果希望查找前1000条数据，正确的做法是取每个片上的前1000条数据，然后将这些数据重新排序"><a href="#如果希望查找前1000条数据，正确的做法是取每个片上的前1000条数据，然后将这些数据重新排序" class="headerlink" title="如果希望查找前1000条数据，正确的做法是取每个片上的前1000条数据，然后将这些数据重新排序"></a>如果希望查找前1000条数据，<code>正确</code>的做法是取每个片上的前1000条数据，然后将这些数据重新排序</h4><ol>
<li>首先在每个数据分片.上都排序并查询前1000条文档。</li>
<li>然后将所有节点的结果聚合，在内存中重新排序选出前1000条文档</li>
<li>最后从这1000条中，选取从990开始的10条文档</li>
</ol>
<p><code>注意</code>：如果搜索页数过深，或者结果集(from + size)越大，对内存和CPU的消耗也越高。因此ES设定结果集查询的.上限是10000</p>
<p>针对深度分页，ES提供了两种<code>解决方案</code>,官方文档:</p>
<p>search after:分页时需要排序，原理是从上一-次的排序值开始，查询下一页数据。官方推荐使用的方式。</p>
<p>scroll:原理将排序数据形成快照，保存在内存。官方已经不推荐使用。</p>
</blockquote>
<p><code>from + size</code>:<br>优点:支持随机翻页<br>缺点:深度分页问题，默认查询上限(from + size)是10000<br>场景:百度、京东、谷歌、淘宝这样的<code>随机翻页搜索</code><br><code>after search</code>:<br>优点:没有查询上限(单次查询的size不超过10000 )<br>缺点:只能向后逐页查询，不支持随机翻页<br>场景:没有随机翻页需求的搜索，例如<code>手机向下滚动翻页</code><br><code>scroll</code>:<br>优点:没有查询.上限(单次查询的size不超过10000)<br>缺点:会有额外内存消耗，并且搜索结果是非实时的<br>场景:海量数据的获取和迁移。从ES7.1开始不推荐，建议用aftersearch方案</p>
<h4 id="高亮"><a href="#高亮" class="headerlink" title="高亮"></a>高亮</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><code class="hljs sql"># 高亮查询<br><span class="hljs-keyword">GET</span> <span class="hljs-operator">/</span>hotel<span class="hljs-operator">/</span>_search<br>&#123;<br>  &quot;query&quot;:&#123;<br>    &quot;match&quot;: &#123;<br>      &quot;all&quot;: &quot;如家&quot;<br>    &#125;<br>  &#125;,<br>  &quot;highlight&quot;:&#123;<br>    <span class="hljs-operator">/</span><span class="hljs-operator">/</span> 指定要高亮的字段<br>    &quot;fields&quot;: &#123;<br>      &quot;all&quot;: &#123;<br>        <span class="hljs-operator">/</span><span class="hljs-operator">/</span> 用来标记高亮字段的前置标签<br>        &quot;pre_tags&quot;: &quot;&lt;em&gt;&quot;,<br>        <span class="hljs-operator">/</span><span class="hljs-operator">/</span> 用来标记高亮字段的后置标签<br>        &quot;post_tags&quot;: &quot;&lt;/em&gt;&quot;<br>      &#125;<br>    &#125;<br>  &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<blockquote>
<h3 id="注意这里不能使用match-all，因为需要指定搜索的关键字"><a href="#注意这里不能使用match-all，因为需要指定搜索的关键字" class="headerlink" title="注意这里不能使用match_all，因为需要指定搜索的关键字"></a>注意这里不能使用<code>match_all</code>，因为需要指定搜索的关键字</h3><p>同时，默认情况下 ES搜索字段必须与高亮字段一致，上述案例中都是<code>all</code>，请看下面案例</p>
</blockquote>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><code class="hljs sql"># 高亮查询<br><span class="hljs-keyword">GET</span> <span class="hljs-operator">/</span>hotel<span class="hljs-operator">/</span>_search<br>&#123;<br>  &quot;query&quot;:&#123;<br>    &quot;match&quot;: &#123;<br>      &quot;all&quot;: &quot;如家&quot;<br>    &#125;<br>  &#125;,<br>  &quot;highlight&quot;:&#123;<br>    <span class="hljs-operator">/</span><span class="hljs-operator">/</span> 指定要高亮的字段<br>    &quot;fields&quot;: &#123;<br>      &quot;name&quot;: &#123;<br>      	<span class="hljs-operator">/</span><span class="hljs-operator">/</span> 搜索字段和高亮字段可以不匹配<br>        &quot;require_field_match&quot;: &quot;false&quot;, <br>        <span class="hljs-operator">/</span><span class="hljs-operator">/</span> 用来标记高亮字段的前置标签<br>        &quot;pre_tags&quot;: &quot;&lt;em&gt;&quot;,<br>        <span class="hljs-operator">/</span><span class="hljs-operator">/</span> 用来标记高亮字段的后置标签<br>        &quot;post_tags&quot;: &quot;&lt;/em&gt;&quot;<br>      &#125;<br>    &#125;<br>  &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h3 id="RestClient查询"><a href="#RestClient查询" class="headerlink" title="RestClient查询"></a>RestClient查询</h3><h4 id="match-all"><a href="#match-all" class="headerlink" title="match_all"></a>match_all</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch.ElasticsearchClient;<br><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch.core.SearchRequest;<br><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch.core.SearchResponse;<br><span class="hljs-keyword">import</span> com.example.demo.domain.HotelDoc;<br><span class="hljs-keyword">import</span> com.example.demo.service.HotelService;<br><span class="hljs-keyword">import</span> lombok.extern.slf4j.Slf4j;<br><span class="hljs-keyword">import</span> org.junit.jupiter.api.Test;<br><span class="hljs-keyword">import</span> org.springframework.beans.factory.annotation.Autowired;<br><span class="hljs-keyword">import</span> org.springframework.boot.test.context.SpringBootTest;<br><br><span class="hljs-keyword">import</span> java.io.IOException;<br><br><span class="hljs-meta">@Slf4j</span><br><span class="hljs-meta">@SpringBootTest</span><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">HotelSearchTest</span> &#123;<br>    <span class="hljs-meta">@Autowired</span><br>    <span class="hljs-keyword">private</span> HotelService hotelService;<br>    <span class="hljs-meta">@Autowired</span><br>    ElasticsearchClient client;<br><br>    <span class="hljs-meta">@Test</span><br>    <span class="hljs-keyword">void</span> <span class="hljs-title function_">matchAllTest</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> IOException &#123;<br>        <span class="hljs-type">SearchRequest</span> <span class="hljs-variable">request</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">SearchRequest</span>.Builder()<br>            	.index(<span class="hljs-string">&quot;hotel&quot;</span>)<br>                .query(q -&gt; q.matchAll(matchAllQuery -&gt; matchAllQuery))<br>                .build();<br>        SearchResponse&lt;HotelDoc&gt; matchSearch = client.search(request, HotelDoc.class);<br>        log.info(<span class="hljs-string">&quot;共搜索到:&#123;&#125;条数据&quot;</span>, (matchSearch.hits().total() == <span class="hljs-literal">null</span> ? <span class="hljs-number">0</span> : matchSearch.hits().total().value()));<br>        matchSearch.hits().hits().forEach(item -&gt; &#123;<br>            System.out.println(item.source());<br>        &#125;);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h4 id="match"><a href="#match" class="headerlink" title="match"></a>match</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch.ElasticsearchClient;<br><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch.core.SearchRequest;<br><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch.core.SearchResponse;<br><span class="hljs-keyword">import</span> com.example.demo.domain.HotelDoc;<br><span class="hljs-keyword">import</span> com.example.demo.service.HotelService;<br><span class="hljs-keyword">import</span> lombok.extern.slf4j.Slf4j;<br><span class="hljs-keyword">import</span> org.junit.jupiter.api.Test;<br><span class="hljs-keyword">import</span> org.springframework.beans.factory.annotation.Autowired;<br><span class="hljs-keyword">import</span> org.springframework.boot.test.context.SpringBootTest;<br><br><span class="hljs-keyword">import</span> java.io.IOException;<br><br><span class="hljs-meta">@Slf4j</span><br><span class="hljs-meta">@SpringBootTest</span><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">HotelSearchTest</span> &#123;<br>    <span class="hljs-meta">@Autowired</span><br>    <span class="hljs-keyword">private</span> HotelService hotelService;<br>    <span class="hljs-meta">@Autowired</span><br>    ElasticsearchClient client;<br><br>    <span class="hljs-meta">@Test</span><br>    <span class="hljs-keyword">void</span> <span class="hljs-title function_">matchTest</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> IOException &#123;<br>        <span class="hljs-type">SearchRequest</span> <span class="hljs-variable">request</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">SearchRequest</span>.Builder()<br>                .index(<span class="hljs-string">&quot;hotel&quot;</span>)<br>                .query(query-&gt;query.match(<br>                        matchQuery-&gt;matchQuery.field(<span class="hljs-string">&quot;brand&quot;</span>).query(<span class="hljs-string">&quot;如家&quot;</span>)<br>                ))<br>                .build();<br>        SearchResponse&lt;HotelDoc&gt; matchSearch = client.search(request, HotelDoc.class);<br>        matchSearch.hits().hits().forEach(item -&gt; &#123;<br>            System.out.println(item.source());<br>        &#125;);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h4 id="term"><a href="#term" class="headerlink" title="term"></a>term</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch.ElasticsearchClient;<br><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch.core.SearchRequest;<br><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch.core.SearchResponse;<br><span class="hljs-keyword">import</span> com.example.demo.domain.HotelDoc;<br><span class="hljs-keyword">import</span> com.example.demo.service.HotelService;<br><span class="hljs-keyword">import</span> lombok.extern.slf4j.Slf4j;<br><span class="hljs-keyword">import</span> org.junit.jupiter.api.Test;<br><span class="hljs-keyword">import</span> org.springframework.beans.factory.annotation.Autowired;<br><span class="hljs-keyword">import</span> org.springframework.boot.test.context.SpringBootTest;<br><br><span class="hljs-keyword">import</span> java.io.IOException;<br><br><span class="hljs-meta">@Slf4j</span><br><span class="hljs-meta">@SpringBootTest</span><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">HotelSearchTest</span> &#123;<br>    <span class="hljs-meta">@Autowired</span><br>    <span class="hljs-keyword">private</span> HotelService hotelService;<br>    <span class="hljs-meta">@Autowired</span><br>    ElasticsearchClient client;<br>	<br>    <span class="hljs-meta">@Test</span><br>    <span class="hljs-keyword">void</span> <span class="hljs-title function_">termQueryTest</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> IOException &#123;<br>        <span class="hljs-type">SearchRequest</span> <span class="hljs-variable">request</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">SearchRequest</span>.Builder()<br>                .index(<span class="hljs-string">&quot;hotel&quot;</span>)<br>                .query(query -&gt; query.term(<br>                        termQuery -&gt; termQuery.field(<span class="hljs-string">&quot;city&quot;</span>).value(<span class="hljs-string">&quot;北京&quot;</span>)<br>                ))<br>                .build();<br>        SearchResponse&lt;HotelDoc&gt; matchSearch = client.search(request, HotelDoc.class);<br>        matchSearch.hits().hits().forEach(item -&gt; &#123;<br>            System.out.println(item.source());<br>        &#125;);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h4 id="range"><a href="#range" class="headerlink" title="range"></a>range</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch.ElasticsearchClient;<br><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch.core.SearchRequest;<br><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch.core.SearchResponse;<br><span class="hljs-keyword">import</span> com.example.demo.domain.HotelDoc;<br><span class="hljs-keyword">import</span> com.example.demo.service.HotelService;<br><span class="hljs-keyword">import</span> lombok.extern.slf4j.Slf4j;<br><span class="hljs-keyword">import</span> org.junit.jupiter.api.Test;<br><span class="hljs-keyword">import</span> org.springframework.beans.factory.annotation.Autowired;<br><span class="hljs-keyword">import</span> org.springframework.boot.test.context.SpringBootTest;<br><br><span class="hljs-keyword">import</span> java.io.IOException;<br><br><span class="hljs-meta">@Slf4j</span><br><span class="hljs-meta">@SpringBootTest</span><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">HotelSearchTest</span> &#123;<br>    <span class="hljs-meta">@Autowired</span><br>    <span class="hljs-keyword">private</span> HotelService hotelService;<br>    <span class="hljs-meta">@Autowired</span><br>    ElasticsearchClient client;<br><br>    <span class="hljs-meta">@Test</span><br>    <span class="hljs-keyword">void</span> <span class="hljs-title function_">termQueryTest</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> IOException &#123;<br>        <span class="hljs-type">SearchRequest</span> <span class="hljs-variable">request</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">SearchRequest</span>.Builder()<br>                .index(<span class="hljs-string">&quot;hotel&quot;</span>)<br>                .query(query -&gt; query.range(<br>                        rangeQuery-&gt;rangeQuery<br>                                .field(<span class="hljs-string">&quot;price&quot;</span>)<br>                                .gte(JsonData.of(<span class="hljs-number">100</span>)).lte(JsonData.of(<span class="hljs-number">300</span>))<br>                ))<br>                .build();<br>        SearchResponse&lt;HotelDoc&gt; matchSearch = client.search(request, HotelDoc.class);<br>        matchSearch.hits().hits().forEach(item -&gt; &#123;<br>            System.out.println(item.source());<br>        &#125;);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h4 id="bool"><a href="#bool" class="headerlink" title="bool"></a>bool</h4><blockquote>
<h1 id="搜索名字包含如家，价格不高于400，在坐标31-21、121-5周围10km范围内的酒店（案例在之前boolean-query部分出现过）"><a href="#搜索名字包含如家，价格不高于400，在坐标31-21、121-5周围10km范围内的酒店（案例在之前boolean-query部分出现过）" class="headerlink" title="搜索名字包含如家，价格不高于400，在坐标31.21、121.5周围10km范围内的酒店（案例在之前boolean query部分出现过）"></a>搜索名字包含<code>如家</code>，价格不高于400，在坐标31.21、121.5周围10km范围内的酒店（案例在之前boolean query部分出现过）</h1></blockquote>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><code class="hljs sql"># 搜索名字包含`如家`，价格不高于<span class="hljs-number">400</span>，在坐标<span class="hljs-number">31.21</span>、<span class="hljs-number">121.5</span>周围<span class="hljs-number">10</span>km范围内的酒店<br><span class="hljs-keyword">GET</span> <span class="hljs-operator">/</span>hotel<span class="hljs-operator">/</span>_search<br>&#123;<br>  &quot;query&quot;:&#123;<br>    &quot;bool&quot;: &#123;<br>      &quot;must&quot;: [<br>        &#123;<br>          &quot;match&quot;: &#123;<br>            &quot;name&quot;: &quot;如家&quot;<br>          &#125;<br>        &#125;<br>      ],<br>      &quot;must_not&quot;: [<br>        &#123;<br>          &quot;range&quot;: &#123;<br>            &quot;price&quot;: &#123;<br>              &quot;gt&quot;: <span class="hljs-number">400</span><br>            &#125;<br>          &#125;<br>        &#125;<br>      ],<br>      &quot;filter&quot;: [<br>        &#123;<br>          &quot;geo_distance&quot;: &#123;<br>            &quot;distance&quot;: &quot;10km&quot;,<br>            &quot;location&quot;: &#123;<br>              &quot;lat&quot;: <span class="hljs-number">31.21</span>,<br>              &quot;lon&quot;: <span class="hljs-number">121.5</span><br>            &#125;<br>          &#125;<br>        &#125;<br>      ]<br>    &#125;<br>  &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch.ElasticsearchClient;<br><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch.core.SearchRequest;<br><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch.core.SearchResponse;<br><span class="hljs-keyword">import</span> com.example.demo.domain.HotelDoc;<br><span class="hljs-keyword">import</span> com.example.demo.service.HotelService;<br><span class="hljs-keyword">import</span> lombok.extern.slf4j.Slf4j;<br><span class="hljs-keyword">import</span> org.junit.jupiter.api.Test;<br><span class="hljs-keyword">import</span> org.springframework.beans.factory.annotation.Autowired;<br><span class="hljs-keyword">import</span> org.springframework.boot.test.context.SpringBootTest;<br><br><span class="hljs-keyword">import</span> java.io.IOException;<br><br><span class="hljs-meta">@Slf4j</span><br><span class="hljs-meta">@SpringBootTest</span><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">HotelSearchTest</span> &#123;<br>    <span class="hljs-meta">@Autowired</span><br>    <span class="hljs-keyword">private</span> HotelService hotelService;<br>    <span class="hljs-meta">@Autowired</span><br>    ElasticsearchClient client;<br><br>    <span class="hljs-meta">@Test</span><br>    <span class="hljs-keyword">void</span> <span class="hljs-title function_">boolQueryTest</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> IOException &#123;<br>        <span class="hljs-type">SearchRequest</span> <span class="hljs-variable">request</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">SearchRequest</span>.Builder()<br>                .index(<span class="hljs-string">&quot;hotel&quot;</span>)<br>                .query(query -&gt; query.bool(<br>                        boolQuery -&gt; boolQuery<br>                                .must(mustQuery -&gt; mustQuery<br>                                        .match(matchQuery -&gt; matchQuery<br>                                                .field(<span class="hljs-string">&quot;name&quot;</span>).query(<span class="hljs-string">&quot;如家&quot;</span>))<br>                                )<br>                                .mustNot(mustNotQuery -&gt; mustNotQuery<br>                                        .range(rangeQuery -&gt; rangeQuery<br>                                                .field(<span class="hljs-string">&quot;price&quot;</span>)<br>                                                .gt(JsonData.of(<span class="hljs-number">400</span>))<br>                                        )<br>                                )<br>                                .filter(filterQuery -&gt; filterQuery<br>                                        .geoDistance(geoDistanceQuery -&gt;<br>                                                geoDistanceQuery<br>                                                        .distance(<span class="hljs-string">&quot;10km&quot;</span>)<br>                                                        .field(<span class="hljs-string">&quot;location&quot;</span>)<br>                                                        .location(geoLocation -&gt; geoLocation<br>                                                                .latlon(<span class="hljs-keyword">new</span> <span class="hljs-title class_">LatLonGeoLocation</span>.Builder()<br>                                                                        .lat(<span class="hljs-number">31.21</span>)<br>                                                                        .lon(<span class="hljs-number">121.5</span>)<br>                                                                        .build()<br>                                                                )<br>                                                        )<br>                                        )<br>                                )<br>                ))<br>                .build();<br>        SearchResponse&lt;HotelDoc&gt; matchSearch = client.search(request, HotelDoc.class);<br>        matchSearch.hits().hits().forEach(item -&gt; &#123;<br>            System.out.println(item.source());<br>        &#125;);<br>    &#125;	<br>&#125;<br></code></pre></td></tr></table></figure>

<blockquote>
<h1 id="也可以使用下面的写法"><a href="#也可以使用下面的写法" class="headerlink" title="也可以使用下面的写法"></a>也可以使用下面的写法</h1><p><code>注意</code>：QueryBuilders的包别导错了</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch.ElasticsearchClient;<br><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch._types.LatLonGeoLocation;<br><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch._types.query_dsl.BoolQuery;<br><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders;<br><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch.core.SearchRequest;<br><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch.core.SearchResponse;<br><span class="hljs-keyword">import</span> co.elastic.clients.json.JsonData;<br><span class="hljs-keyword">import</span> com.example.demo.domain.HotelDoc;<br><span class="hljs-keyword">import</span> lombok.extern.slf4j.Slf4j;<br><span class="hljs-keyword">import</span> org.junit.jupiter.api.Test;<br><span class="hljs-keyword">import</span> org.springframework.beans.factory.annotation.Autowired;<br><span class="hljs-keyword">import</span> org.springframework.boot.test.context.SpringBootTest;<br><br><span class="hljs-keyword">import</span> java.io.IOException;<br><br><span class="hljs-meta">@Slf4j</span><br><span class="hljs-meta">@SpringBootTest</span><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">HotelSearchTest</span> &#123;<br>    <span class="hljs-meta">@Autowired</span><br>    ElasticsearchClient client;<br><br>    <span class="hljs-meta">@Test</span><br>    <span class="hljs-keyword">void</span> <span class="hljs-title function_">boolQueryTest</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> IOException &#123;<br>        <span class="hljs-type">BoolQuery</span> <span class="hljs-variable">build</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">BoolQuery</span>.Builder()<br>                .must(QueryBuilders.term().field(<span class="hljs-string">&quot;name&quot;</span>).value(<span class="hljs-string">&quot;如家&quot;</span>).build()._toQuery())<br>                .mustNot(QueryBuilders.range().field(<span class="hljs-string">&quot;price&quot;</span>).gt(JsonData.of(<span class="hljs-number">400</span>)).build()._toQuery())<br>                .filter(QueryBuilders.geoDistance().field(<span class="hljs-string">&quot;location&quot;</span>)<br>                        .distance(<span class="hljs-string">&quot;10km&quot;</span>)<br>                        .location(item -&gt; item.latlon(<span class="hljs-keyword">new</span> <span class="hljs-title class_">LatLonGeoLocation</span>.Builder().lat(<span class="hljs-number">31.21</span>).lon(<span class="hljs-number">121.5</span>).build()))<br>                        .build()._toQuery())<br>                .build();<br>        <span class="hljs-type">SearchRequest</span> <span class="hljs-variable">request</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">SearchRequest</span>.Builder()<br>                .index(<span class="hljs-string">&quot;hotel&quot;</span>)<br>                .query(build._toQuery())<br>                .build();<br>        SearchResponse&lt;HotelDoc&gt; matchSearch = client.search(request, HotelDoc.class);<br>        matchSearch.hits().hits().forEach(item -&gt; &#123;<br>            System.out.println(item.source());<br>        &#125;);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>fuzzy</p>
<blockquote>
<p>模糊查询（fuzzy query）是一种搜索技术，它允许在搜索时使用模糊匹配，以便在搜索结果中包含与搜索词项相似但不完全匹配的项。模糊查询通常用于处理拼写错误、同义词、缩写词、音近字等情况。在实现模糊查询时，常用的算法包括编辑距离算法、n-gram算法、Soundex算法等。在搜索引擎、数据库、信息检索等领域中，模糊查询是一种常见的技术手段。</p>
</blockquote>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">GET</span> <span class="hljs-operator">/</span>hotel<span class="hljs-operator">/</span>_search<br>&#123;<br>  &quot;query&quot;:&#123;<br>    &quot;fuzzy&quot;: &#123;<br>      &quot;brand&quot;: &#123;<br>        &quot;fuzziness&quot;: &quot;2&quot;,<br>        &quot;value&quot;: &quot;儒家&quot;<br>      &#125;<br>    &#125;<br>  &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch.ElasticsearchClient;<br><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch._types.LatLonGeoLocation;<br><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch._types.query_dsl.BoolQuery;<br><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders;<br><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch.core.SearchRequest;<br><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch.core.SearchResponse;<br><span class="hljs-keyword">import</span> co.elastic.clients.json.JsonData;<br><span class="hljs-keyword">import</span> com.example.demo.domain.HotelDoc;<br><span class="hljs-keyword">import</span> lombok.extern.slf4j.Slf4j;<br><span class="hljs-keyword">import</span> org.junit.jupiter.api.Test;<br><span class="hljs-keyword">import</span> org.springframework.beans.factory.annotation.Autowired;<br><span class="hljs-keyword">import</span> org.springframework.boot.test.context.SpringBootTest;<br><br><span class="hljs-keyword">import</span> java.io.IOException;<br><br><span class="hljs-meta">@Slf4j</span><br><span class="hljs-meta">@SpringBootTest</span><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">HotelSearchTest</span> &#123;<br>    <span class="hljs-meta">@Autowired</span><br>    ElasticsearchClient client;<br><br>    <span class="hljs-meta">@Test</span><br>    <span class="hljs-keyword">void</span> <span class="hljs-title function_">fuzzyQueryTest</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> IOException &#123;<br>        SearchResponse&lt;HotelDoc&gt; request = client.search(searchRequest -&gt; searchRequest<br>                .index(<span class="hljs-string">&quot;hotel&quot;</span>)<br>                .query(query -&gt; query<br>                        .fuzzy(fuzzyQuery -&gt; fuzzyQuery<br>                                .field(<span class="hljs-string">&quot;brand&quot;</span>)<br>                                .fuzziness(<span class="hljs-string">&quot;auto&quot;</span>)<br>                                .value(<span class="hljs-string">&quot;儒家&quot;</span>)<br>                        )<br>                ), HotelDoc.class);<br>        log.info(<span class="hljs-string">&quot;共搜索到:&#123;&#125;条数据&quot;</span>, (request.hits().total() == <span class="hljs-literal">null</span> ? <span class="hljs-number">0</span> : request.hits().total().value()));<br>        request.hits().hits().forEach(item -&gt; &#123;<br>            System.out.println(item.source());<br>        &#125;);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h4 id="排序和分页"><a href="#排序和分页" class="headerlink" title="排序和分页"></a>排序和分页</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch.ElasticsearchClient;<br><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch._types.LatLonGeoLocation;<br><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch._types.SortOrder;<br><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch._types.query_dsl.BoolQuery;<br><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders;<br><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch.core.SearchRequest;<br><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch.core.SearchResponse;<br><span class="hljs-keyword">import</span> co.elastic.clients.json.JsonData;<br><span class="hljs-keyword">import</span> com.example.demo.domain.HotelDoc;<br><span class="hljs-keyword">import</span> lombok.extern.slf4j.Slf4j;<br><span class="hljs-keyword">import</span> org.junit.jupiter.api.Test;<br><span class="hljs-keyword">import</span> org.springframework.beans.factory.annotation.Autowired;<br><span class="hljs-keyword">import</span> org.springframework.boot.test.context.SpringBootTest;<br><br><span class="hljs-keyword">import</span> java.io.IOException;<br><br><span class="hljs-meta">@Slf4j</span><br><span class="hljs-meta">@SpringBootTest</span><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">HotelSearchTest</span> &#123;<br>    <span class="hljs-meta">@Autowired</span><br>    ElasticsearchClient client;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 分页、排序测试</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-meta">@Test</span><br>    <span class="hljs-keyword">void</span> <span class="hljs-title function_">pageAndSortTest</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> IOException &#123;<br>        <span class="hljs-comment">// 页码、每页信息条数</span><br>        <span class="hljs-type">int</span> <span class="hljs-variable">page</span> <span class="hljs-operator">=</span> <span class="hljs-number">1</span>, size = <span class="hljs-number">10</span>;<br>        <span class="hljs-type">SearchRequest</span> <span class="hljs-variable">request</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">SearchRequest</span>.Builder()<br>                .index(<span class="hljs-string">&quot;hotel&quot;</span>)<br>                .query(builder -&gt; builder.match(matchAllQuery -&gt; matchAllQuery.field(<span class="hljs-string">&quot;brand&quot;</span>).query(<span class="hljs-string">&quot;如家&quot;</span>)))<br>                .from((page - <span class="hljs-number">1</span>) * size)<br>                .size(size)<br>                .sort(sortOptions -&gt; sortOptions.field(fieldSort -&gt; fieldSort.field(<span class="hljs-string">&quot;price&quot;</span>).order(SortOrder.Asc)))<br>                .build();<br>        SearchResponse&lt;HotelDoc&gt; search = client.search(request, HotelDoc.class);<br>        log.info(<span class="hljs-string">&quot;共搜索到:&#123;&#125;条数据&quot;</span>, (search.hits().total() == <span class="hljs-literal">null</span> ? <span class="hljs-number">0</span> : search.hits().total().value()));<br>        search.hits().hits().forEach(item -&gt; &#123;<br>            System.out.println(item.source());<br>        &#125;);<br>    &#125;<br><br>&#125;<br></code></pre></td></tr></table></figure>

<h4 id="高亮-1"><a href="#高亮-1" class="headerlink" title="高亮"></a>高亮</h4><blockquote>
<h1 id="总体来说不算太难，对应sql语句使用idea的提示基本就可以凑出来"><a href="#总体来说不算太难，对应sql语句使用idea的提示基本就可以凑出来" class="headerlink" title="总体来说不算太难，对应sql语句使用idea的提示基本就可以凑出来"></a>总体来说不算太难，对应sql语句使用idea的提示基本就可以凑出来</h1></blockquote>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">GET</span> <span class="hljs-operator">/</span>hotel<span class="hljs-operator">/</span>_search<br>&#123;<br>  &quot;query&quot;:&#123;<br>    &quot;match&quot;: &#123;<br>      &quot;all&quot;: &quot;如家&quot;<br>    &#125;<br>  &#125;,<br>  &quot;highlight&quot;:&#123;<br>    &quot;fields&quot;: &#123;<br>      &quot;name&quot;: &#123;<br>        &quot;require_field_match&quot;: &quot;false&quot;, <br>        &quot;pre_tags&quot;:&quot;&lt;em&gt;&quot;,<br>        &quot;post_tags&quot;: &quot;&lt;/em&gt;&quot;<br>      &#125;<br>    &#125;<br>  &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch.ElasticsearchClient;<br><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch._types.LatLonGeoLocation;<br><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch._types.SortOrder;<br><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch._types.query_dsl.BoolQuery;<br><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders;<br><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch.core.SearchRequest;<br><span class="hljs-keyword">import</span> co.elastic.clients.elasticsearch.core.SearchResponse;<br><span class="hljs-keyword">import</span> co.elastic.clients.json.JsonData;<br><span class="hljs-keyword">import</span> com.example.demo.domain.HotelDoc;<br><span class="hljs-keyword">import</span> lombok.extern.slf4j.Slf4j;<br><span class="hljs-keyword">import</span> org.junit.jupiter.api.Test;<br><span class="hljs-keyword">import</span> org.springframework.beans.factory.annotation.Autowired;<br><span class="hljs-keyword">import</span> org.springframework.boot.test.context.SpringBootTest;<br><br><span class="hljs-keyword">import</span> java.io.IOException;<br><br><span class="hljs-meta">@Slf4j</span><br><span class="hljs-meta">@SpringBootTest</span><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">HotelSearchTest</span> &#123;<br>    <span class="hljs-meta">@Autowired</span><br>    ElasticsearchClient client;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 高亮测试</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-meta">@Test</span><br>    <span class="hljs-keyword">void</span> <span class="hljs-title function_">highLightTest</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> IOException &#123;<br>        <span class="hljs-type">SearchRequest</span> <span class="hljs-variable">request</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">SearchRequest</span>.Builder()<br>                .index(<span class="hljs-string">&quot;hotel&quot;</span>)<br>                .query(query-&gt;query.match(matchQuery-&gt;matchQuery.field(<span class="hljs-string">&quot;all&quot;</span>).query(<span class="hljs-string">&quot;如家&quot;</span>)))<br>                .highlight(highLight-&gt;highLight.fields(<span class="hljs-string">&quot;name&quot;</span>,builder -&gt; builder.preTags(<span class="hljs-string">&quot;&lt;em&gt;&quot;</span>).postTags(<span class="hljs-string">&quot;&lt;/em&gt;&quot;</span>)).requireFieldMatch(<span class="hljs-literal">false</span>))<br>                .build();<br>        SearchResponse&lt;HotelDoc&gt; search = client.search(request, HotelDoc.class);<br>        log.info(<span class="hljs-string">&quot;共搜索到:&#123;&#125;条数据&quot;</span>, (search.hits().total() == <span class="hljs-literal">null</span> ? <span class="hljs-number">0</span> : search.hits().total().value()));<br>        search.hits().hits().forEach(item -&gt; &#123;<br>            item.highlight().values().forEach(System.out::println);<br>        &#125;);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/elasticsearch/" class="category-chain-item">elasticsearch</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/elasticsearch/">#elasticsearch</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>elasticsearch学习</div>
      <div>http://example.com/2023/05/30/编程语言/Java/学习/框架/Elasticsearct/Elasticsearct学习/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>深海火锅店</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2023年5月30日</div>
        </div>
      
      
      
        <div class="license-meta-item">
          <div>许可协议</div>
          <div>
            
              
              
                <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
                  <span class="hint--top hint--rounded" aria-label="BY - 署名">
                    <i class="iconfont icon-by"></i>
                  </span>
                </a>
              
            
          </div>
        </div>
      
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2023/06/02/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/React/01.React%E5%9F%BA%E7%A1%80/" title="React基础">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">React基础</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2023/05/28/%E5%AE%9E%E7%94%A8%E5%B0%8F%E6%8A%80%E5%B7%A7/%E7%BC%96%E7%A8%8B/Map%E4%B8%8E%E5%AE%9E%E4%BD%93%E8%BD%AC%E6%8D%A2/Map%E4%B8%8E%E5%AE%9E%E4%BD%93%E8%BD%AC%E6%8D%A2/" title="map与实体互相转换">
                        <span class="hidden-mobile">map与实体互相转换</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
    <div class="statistics">
  
  

  
    
      <span id="busuanzi_container_site_pv" style="display: none">
        总访问量 
        <span id="busuanzi_value_site_pv"></span>
         次
      </span>
    
    
      <span id="busuanzi_container_site_uv" style="display: none">
        总访客数 
        <span id="busuanzi_value_site_uv"></span>
         人
      </span>
    
    
  
</div>

  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init(Object.assign({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      scrollSmooth    : true,
      includeTitleTags: true,
      headingsOffset  : -boardTop,
    }, CONFIG.toc));
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }

    Fluid.events.registerRefreshCallback(function() {
      if ('tocbot' in window) {
        tocbot.refresh();
        var toc = jQuery('#toc');
        if (toc.length === 0 || !tocbot) {
          return;
        }
        if (toc.find('.toc-list-item').length > 0) {
          toc.css('visibility', 'visible');
        }
      }
    });
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.11/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));

    Fluid.events.registerRefreshCallback(function() {
      if ('anchors' in window) {
        anchors.removeAll();
        var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
        var res = [];
        for (var item of el) {
          res.push('.markdown-body > ' + item.trim());
        }
        if (CONFIG.anchorjs.placement === 'left') {
          anchors.options.class = 'anchorjs-link-left';
        }
        anchors.add(res.join(', '));
      }
    });
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>

  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>




  
<script src="//cdn.jsdelivr.net/gh/EmoryHuang/BlogBeautify@1.1/DynamicRibbon.min.js"></script>



<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
